JamesDooh JamesDooh - 1 year ago 80
MySQL Question

SQL error syntax on query with variables

I am trying to create an import script that looks as below:

foreach ( $app_files as $file ) {
if ( $row["old_id_import"] == $file['idx'] ) {

$ids = $row["id"];
$files = $file['file'];
$date = date( 'Y-m-d H:i:s' );
$sql2 = "INSERT INTO palace_files ('id', 'module', 'item_id', 'name', 'status', 'date', 'modified') VALUES (NULL, 5, "'.$ids.'", "'.$files.'",1 ,"'.$date.'" , "'.$date.'")";
if ($conn->query($sql2) === TRUE) {
echo "New record imported successfully";
} else {
echo "Error: " . $sql2 . "<br>" . $conn->error;

But it seems that i have syntax error, dont know where. Maybe i am tired.

I have found similar threads but none worked.

Answer Source

You query has some mistakes.

  1. module, status and date are reserved words in sql, so wrap them in ``
  2. you should not wrap the columns in ' '
  3. you are not wrapping them with ' ' in your values

    $sql2 = "INSERT INTO palace_files (id, module, item_id, name, status, date, modified) VALUES (NULL, 5, '{$ids}', '{$files}',1 ,'{$date}' , '{$date}')";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download