0

Error updating record: 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 '? WHERE user_id <> ? AND 1 = 1' at line 1

Here is my code:

<?php
$dbhost      = "localhost";
$dbuser      = "*******";
$dbpass      = "********";
$dbname      = "************";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$newcredit = $_POST["newLOC"]; 
$UID = 1;

   if(! $conn ) {
      die('Could not connect: ' . mysqli_error());
   }
   //echo 'Connected successfully<br>';
   $sql = 'UPDATE users SET line_of_credit = ? WHERE user_id <> ? AND 1 = 1';
   $stmt = $conn->prepare($sql);
   $stmt->bind_param('ii', $newcredit, $UID);
   $stmt->execute();
     
   if (mysqli_query($conn, $sql)) {
      echo "Record updated successfully";
   } else {
      echo "Error updating record: " . mysqli_error($conn);
   }
   mysqli_close($conn);
?>
<?php 
}
?>

The thing is, it IS updating the table, so I'm not sure why I'm getting the error message. When I run the code with values in the database itself, it runs fine.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Blondie
  • 59
  • 5
  • 2
    You are trying to run the prepared statement again in `if (mysqli_query($conn, $sql)) {`. – Nigel Ren Aug 10 '20 at 18:52
  • 1
    Have a look at https://stackoverflow.com/a/8927601/1213708 which also shows how you can check data has actually been updated. – Nigel Ren Aug 10 '20 at 18:57
  • 1
    You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Aug 10 '20 at 19:54
  • Nigel, I removed the 2nd if statement and no more errors. Thank you! – Blondie Aug 10 '20 at 20:16

0 Answers0