What is wrong with this query ?

Hi!

CREATE TABLE
    settings
    (
        KEY VARCHAR(255) COLLATE latin1_danish_ci NOT NULL,
        val VARCHAR(255) COLLATE latin1_danish_ci NOT NULL,
        golf_id INT DEFAULT '0' NOT NULL,
        USER VARCHAR(30) COLLATE latin1_danish_ci NOT NULL,
        password VARCHAR(30) COLLATE latin1_danish_ci NOT NULL,
        regdate DATE
    )

Open in new window


When i try to insert a new record i get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='default_template', val='Golf template 1', golf_id=9' at line 1INSERT INTO `settings` SET key='default_template', val='Golf template 1', golf_id=9

My PHP Code for this query is :
			// Settings
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET key='default_template', val='Golf template 1', golf_id=$golfclub_id ";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET key='bounce_email', val='$bemail', golf_id=$golfclub_id";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET key='from_email', val='$femail', golf_id=$golfclub_id, regdate = $join_date ";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET key='from_name', val='$navnklubben', golf_id=$golfclub_id, regdate = $join_date ";
		        $res = query($sql,$db);

Open in new window


                   
What is wrong here ?
LVL 2
team2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I think the INSERT and UPDATE syntax may be confused.  IIRC UPDATE uses SET, but not INSERT.

Examples of the syntax for these statements are in this article (or linked from this article):
http://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Also, from the look of the CREATE TABLE statement, there is a column named KEY, but not a column named key.  Be on the lookout for case-sensitive names.
0
Julian HansenCommented:
Building on what Ray sayd Try this:

$sql = "INSERT INTO `"._DB_PREFIX."settings`(`KEY`, `val`, `golf_id`)  VALUES('default_template','Golf template 1', $golfclub_id)";

Open in new window


You can use the above as a template for your other queries.
0
Julian HansenCommented:
Just noticed another thing
CREATE TABLE
    settings
    (
        KEY VARCHAR(255) COLLATE latin1_danish_ci NOT NULL,
        val VARCHAR(255) COLLATE latin1_danish_ci NOT NULL,
        golf_id INT DEFAULT '0' NOT NULL,
        USER VARCHAR(30) COLLATE latin1_danish_ci NOT NULL,  /* REQUIRED */
        password VARCHAR(30) COLLATE latin1_danish_ci NOT NULL, /* REQUIRED*/
        regdate DATE
    )

Open in new window

The USER and password fields are set as NOT NULL - therefore you have to specify a value for those in your INSERT query.

That means either put the values into the query or use blanks like so

$sql = "INSERT INTO `"._DB_PREFIX."settings`(`KEY`, `val`, `golf_id`,`USER`, `password`)  
     VALUES('default_template','Golf template 1', $golfclub_id, '','')"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

team2005Author Commented:
Hi!

Tryed to insert user and password to, but get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='default_template', val='Golf template 1', golf_id=9' at line 1INSERT INTO `settings` SET user='bamle',password='bamle',key='default_template', val='Golf template 1', golf_id=9


				// Settings
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user='$brukernavn',password='$passord',key='default_template', val='Golf template 1', golf_id=$golfclub_id ";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='bounce_email', val='$bemail', golf_id=$golfclub_id";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='from_email', val='$femail', golf_id=$golfclub_id, regdate = $join_date ";
		        $res = query($sql,$db);
				
				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='from_name', val='$navnklubben', golf_id=$golfclub_id, regdate = $join_date ";
		        $res = query($sql,$db);
		        

Open in new window

0
Julian HansenCommented:
Ok but you are still using the incorrect syntax for the insert.

The SET directive is used in UPDATE statements - my previous post demonstrates how to construct an INSERT statement.

Incorrect
$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user='$brukernavn',password='$passord',key='default_template', val='Golf template 1', golf_id=$golfclub_id ";

Open in new window

Correct
$sql = "INSERT INTO `"._DB_PREFIX."settings`(`KEY`, `val`, `golf_id`,`USER`, `password`)  
     VALUES('default_template','Golf template 1', $golfclub_id, '','')"

Open in new window

0
team2005Author Commented:
thanks
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.