I'm using PHP/MySQL to store data collected from a web page. 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 narrative_photos VALUES (`filename`, `narrative_id`) VALUES ('ash_02
When I take the statement produced by PHP and paste it into the MySQL console, the statement works fine.
Here's the PHP code:
foreach ($files['pictures']['final_name'] as $key => $final_name) {
$sql .= "INSERT INTO narrative_photos ";
$sql .= "(`filename`, `narrative_id`) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $final_name) . "', ";
$sql .= "'LAST_INSERT_ID()'); ";
}
It produces something that looks like this:
INSERT INTO narrative_photos VALUES (`filename`, `narrative_id`) VALUES ('ash_020819-140257.png', 3);
If I paste that into MySQL it works. But if I comment out the PHP code and substitute:
$sql .= "INSERT INTO narrative_photos VALUES (`filename`, `narrative_id`) VALUES ('ash_020819-140257.png', 3);";
it continues to throw the MySQL error.
I've been playing with this for a couple of hours and I can't figure out where my mistake is. I would appreciate a second set of eyes. Thanks!
EDIT: Here's the entire function for context.
function insert_narrative($narrative, $files) {
global $db;
$sql = "INSERT INTO narratives ";
$sql .= "(date, positive_thing, what_you_did, goals, plan, entered_by, library_id) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $narrative['sqldate']) . "', ";
$sql .= "'" . db_escape($db, $narrative['positive_thing']) . "', ";
$sql .= "'" . db_escape($db, $narrative['what_you_did']) . "', ";
$sql .= "'" . db_escape($db, $narrative['goals']) . "', ";
$sql .= "'" . db_escape($db, $narrative['plan']) . "', ";
$sql .= "'" . db_escape($db, $narrative['entered_by']) . "', ";
$sql .= "'" . db_escape($db, $_SESSION['library_id']) . "'";
$sql .= "); ";
if (!empty($files['pictures']['final_name'])) {
foreach ($files['pictures']['final_name'] as $key => $final_name) {
$sql .= "INSERT INTO narrative_photos ";
$sql .= "(`filename`, `narrative_id`) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $final_name) . "', ";
$sql .= "LAST_INSERT_ID()); ";
}
}
$result = mysqli_query($db, $sql);
if ($result) {
return true;
} else {
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
EDIT #2: I just realized that independent of the syntax error my approach isn't going to work because that LAST_INSERT_ID is probably going to pick up the ids for each of those inserts instead of just using the id from the main table. I've modified the function but I'm still getting a syntax error at SET @narrative_id. Here's the code.
$sql = "INSERT INTO narratives ";
$sql .= "(date, positive_thing, what_you_did, goals, plan, entered_by, library_id) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $narrative['sqldate']) . "', ";
$sql .= "'" . db_escape($db, $narrative['positive_thing']) . "', ";
$sql .= "'" . db_escape($db, $narrative['what_you_did']) . "', ";
$sql .= "'" . db_escape($db, $narrative['goals']) . "', ";
$sql .= "'" . db_escape($db, $narrative['plan']) . "', ";
$sql .= "'" . db_escape($db, $narrative['entered_by']) . "', ";
$sql .= "'" . db_escape($db, $_SESSION['library_id']) . "'";
$sql .= "); ";
$sql .= "SET @narrative_id = LAST_INSERT_ID()";
if (!empty($files['pictures']['final_name'])) {
foreach ($files['pictures']['final_name'] as $key => $final_name) {
$sql .= "INSERT INTO narrative_photos ";
$sql .= "(`filename`, `narrative_id`) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $final_name) . "', ";
$sql .= "@narrative_id); ";
}
}
'LAST_INSERT_ID()'in fact if you can remove that completely. As the key is automatically done on inserts. Basically your passing a string to the Pkey of the table. Which is why you get an error.LAST_INSERT_ID()is that I have a SQL statement preceding this that writes to a related table.LAST_INSERT_IDis a MySql function, it was like putting quotes aroundDATE(datetime)orWHERE.echo $sql;just before the closing for loop and see what you are getting as sql.