Pangamma
Pangamma

Reputation: 807

INSERT INTO php mysql statement is not working due to incorrect syntax.

Trying to figure out why the following code produces 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 ''username' , 'password' ) VALUES ('Pangamma', '39ef1e6d2fb0743000e5956362b6dc55'' at line 1

The password, email, and user are all correct. So are the database settings. I've been whacking my head on a wall for 3 hours now. Probably something stupidly simple. What am I doing wrong?

/** returns a reason if it fails **/
        function addUser($user,$password,$email){
            global $db_database;global $tb_logins;global $tb_info; global $tb_licenses;
            $licensekey = md5($user.$password.$email);
            $hash = md5($user.'please'.$password.'the salt');
            if (mysql_num_rows(mysql_query("SELECT * FROM ".$tb_logins." WHERE username='$user'")) > 0){
                return 'error : username already in use.';
            }
            mysql_query("INSERT INTO `$db_database`.`$tb_logins` ( 'username' , 'password' ) VALUES ('$user', '$hash')") or die(mysql_error());
            mysql_query("INSERT INTO $tb_info (`username`,`email`,`licensekey`,`limit`) VALUES ('".$user."','".$email."','".$licensekey."','0')") or die(mysql_error());
            return 'success!';
        }

Upvotes: 0

Views: 436

Answers (4)

Parixit
Parixit

Reputation: 3855

Single quot(') is only use for values, not for selecting columns.

you can use backtik (`) like following

mysql_query("INSERT INTO `$db_database`.`$tb_logins` (`username` ,`password`) VALUES ('$user', '$hash')") or die(mysql_error());

OR you can place it without anything

mysql_query("INSERT INTO `$db_database`.`$tb_logins` (username ,password) VALUES ('$user', '$hash')") or die(mysql_error());

And for second query you have used limit as a column name. Please don't use it because it is keyword of SQL. So it will give you syntax error.

Upvotes: 5

rams0610
rams0610

Reputation: 1051

Change this line

mysql_query("INSERT INTO $db_database.$tb_logins ( 'username' , 'password' ) VALUES ('$user', '$hash')") or die(mysql_error());

to

    mysql_query("INSERT INTO `$db_database`.`$tb_logins` ( `username` , `password` ) VALUES ('$user', '$hash')") or die(mysql_error());

Upvotes: 2

Vimal Bera
Vimal Bera

Reputation: 10497

Change your first mysql_query statement to this :

mysql_query("INSERT INTO '$db_database'.'$tb_logins' ( username , password ) VALUES ('$user', '$hash')") or die(mysql_error());

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You have to use backtick around column name, not single quote.

INSERT INTO `$db_database`.`$tb_logins` ( 'username' , 'password' )

should be

INSERT INTO `$db_database`.`$tb_logins` ( `username` , `password` )

Upvotes: 3

Related Questions