2

I am making myself a website and I am onto the backend administration section, I'm currently stuck on submitting the details from a form into my database, however I keep getting the same error over and over again no matter what, which is:

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 'Change) VALUES ('Connor', >'Connor')' at line 1

This is the code which is giving me the error (it connects perfectly to the database):

$query = "INSERT INTO changes (DevName,Change) VALUES ('$dev', '$changed')";

$a = mysql_query($query);

It keeps saying syntax error when I have been looking at other code, and it shows the exact same thing as the code that I have (except the variables of course).

  • `Change` is a reserved word in the `SQL` language. You have to put backticks around it: ` Change `. – arkascha Aug 23 '15 at 12:39
  • @arkascha That did it, that worked. Thanks! I'm getting _1 record added_ output now! – Basics Coding Aug 23 '15 at 12:41
  • 1
    You are welcome. Have fun :-) – arkascha Aug 23 '15 at 12:42
  • Fred, I had no idea what was wrong with it, so how could I possibly find that answer? – Basics Coding Aug 23 '15 at 13:55
  • 1
    @Fred-ii- is a virtual robot living here on SO. So he knows all questions by heart. – arkascha Aug 23 '15 at 13:57
  • `$a = mysql_query($query) or die(mysql_error());` - `...right syntax to use near 'Change)` is what you would have seen. Most common error made; not checking for errors. It's said countless time. If I got nickel for everytime that's said; we'd all retire ;-) Edit: Just like the error stated and told you. – Funk Forty Niner Aug 23 '15 at 14:03
  • *"Fred, I had no idea what was wrong with it, so how could I possibly find that answer?"* - **A:** You Google your error? ;-) and for probable subsequent errors? In turn possibly avoiding questions and for you to learn how to debug code. No better way to learn than *from your mistakes* ;-) – Funk Forty Niner Aug 23 '15 at 14:08

2 Answers2

2

That's cause Change is a reserve word in MySQL.. You will have to escape it using backtique.

The following works fine, otherwise explosions:

create table changes
(   DevName varchar(100),
    `Change` varchar(100)
);

insert changes(DevName,`Change`) values ('1','2');
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

Please don't use mysql_* methods because they are deprecated in PHP5.5 and removed in PHP7. Instead of that you should use PDO or mysqli

$sth = $dbh->prepare('INSERT INTO changes ('DevName', 'Change') VALUES (:dev, :changed)');
$sth->bindParam(':dev', $dev, PDO::PARAM_STR);
$sth->bindParam(':changed', $changed, PDO::PARAM_BOOL);
$sth->execute();

For more details about PDO visit site http://php.net/manual/en/book.pdo.php

  • http://stackoverflow.com/questions/4764908/pdostatement-use-grave-accents-in-columns – Drew Aug 23 '15 at 12:50