Error in SQL syntax in PHP

Please see below code,

$friendslist = "232,72,284,345";
$id = 1028;
$friendarray = explode(",", $friendslist);
$frienduserarray = array();

for ($n = 0; $n < count($friendarray); $n++) {
      $friendidpush = "('".$id."','".$friendarray[$n]."'),";
      array_push($frienduserarray, $friendidpush);
}
$query = "INSERT INTO freddyhipment (id, order) VALUES ";
$friendarray = explode(",", $friendslist);

foreach ($friendarray as $order) {
    $query .= "('" . $id . "','" . $order . "'),";
}

$query = substr($query, 0, -1); // remove trailing comma

			if (mysqli_query($conn,$query)) {

						    echo "Data saved in Database successfully,Clik to go in <a href='index.php'>System</a><br /> ";
        }
  else {
            echo "Data not saved,Clik to go in <a href='index.php'>System</a>" . mysqli_error($conn);
        }

I’m getting below error. Please help.

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near β€˜order) VALUES (β€˜1028’,β€˜232’),(β€˜1028’,β€˜72’),(β€˜1028’,β€˜284’),(β€˜1028’,β€˜345’)’ at line 1

order is a reserved keyword (=> ORDER BY)

1 Like

Oops, I’m sorry. Thanks is help.

Note on interpreting those error messages: the SQL syntax issue is either immediately before or immediately after where the quoted SQL starts.

Try echoing the query then copy and paste into phpmyadmin.

Linefeeds can be added and errors are easier to spot.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.