Alexander Freyr
Alexander Freyr

Reputation: 918

MySQL Error - "You have an error in your SQL syntax" on insert

The error message I'm getting:

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 'INSERT INTO accounts(balance, interest) VALUES(0, 1.5)' at line 4 in INSERT INTO accounts(id_user, interest) VALUES(73, 'Savings'); INSERT INTO balance(balance, interest) VALUES(0, 1.5)

My PHP code is:

$query = "INSERT INTO accounts(`id_user`, `type`)
          VALUES($userid, '$type');

          INSERT INTO balance(`balance`, `interest`)
          VALUES(0, $interest)";

My first guess that something was wrong with my query, so I tried to run the exact same query in phpMyAdmin and it worked perfectly.

Any suggestions on what might be wrong ?

Upvotes: 0

Views: 1997

Answers (2)

thepiyush13
thepiyush13

Reputation: 1331

Are you using mysqli to run this ? I suspect you are running two queries in a single statement, you need to use mysqli_multi_query function to execute multiple queries at the same time.

Mysqli Manual page on multi_query

Upvotes: 2

bassxzero
bassxzero

Reputation: 5041

Gordon Linoff is correct.

From the great manual in the sky.

"mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier. "

http://php.net/manual/en/function.mysql-query.php

change

$query = "INSERT INTO accounts(`id_user`, `type`)
          VALUES($userid, '$type');

          INSERT INTO balance(`balance`, `interest`)
          VALUES(0, $interest)";

to

$query = "INSERT INTO accounts(`id_user`, `type`)
          VALUES($userid, '$type');";
result = mysql_query($query);

$query="INSERT INTO balance(`balance`, `interest`)
          VALUES(0, $interest)";
result = mysql_query($query);

Upvotes: 2

Related Questions