-1

Am trying to execute this function to post data to followup table

$ob=$_POST['ob'];
$update=$_POST['update'];
$updating_officer=$_POST['updating_officer'];
$update_date=$_POST['update_date'];
{
$query="insert into followup (ob,update,updating_officer,update_date) values(?,?,?,?)";
$stmt = $mysqli->prepare($query);
$rc=$stmt->bind_param('isss',$ob,$update,$updating_officer,$update_date);
$stmt->execute();
echo"<script>alert('Update on case $ob recorded successfully;</script>";
}

but this error shows up:

Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\ob\admin\update.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\ob\admin\update.php on line 17

WHat am i doing wrong

Álvaro González
  • 135,557
  • 38
  • 250
  • 339
  • The `{` and `}` dont need to be there – RiggsFolly May 31 '22 at 13:24
  • 2
    `update` is a reserved word and therefore you have to wrap it in backticks like `insert into followup (ob,\`update\`,updating_officer,update_date)` if you use it as a column name. Therefore the prepare will have failed and set `$stmt` to false (a boolean). Its best NOT to use reserved words as column or table names – RiggsFolly May 31 '22 at 13:26
  • Removed them, but no change – Meshack Mutunga May 31 '22 at 13:27
  • Most probably your SQL Statement is faulty. Please check if prepare has any errors: `$stmt = $mysqli->prepare($query); if ($stmt === false) { die('prepare() failed: ' . htmlspecialchars($mysqli->error)); } ` What @RiggsFolly said is probably the cause of the error. – Laisender May 31 '22 at 13:28
  • See the next comment – RiggsFolly May 31 '22 at 13:28
  • MySQLi does not report errors by default unless you're using a recent PHP version. Please check [How to report errors in mysqli](https://phpdelusions.net/mysqli/error_reporting) for further details. – Álvaro González May 31 '22 at 13:31
  • WOW, thanks @RiggsFolly it worked – Meshack Mutunga May 31 '22 at 13:31
  • The [Reserved words](https://dev.mysql.com/doc/refman/8.0/en/keywords.html) – RiggsFolly May 31 '22 at 13:32
  • @Laisender the problem was in the update part, which is a reserved work.. Used backlashes and it worked – Meshack Mutunga May 31 '22 at 13:33
  • I know. Just remember to always have a check if the sql was faulty and if so, iniate a abort routine like displaying a message `Error: blabla`. In this way, you and the user always knows that there was a error. You can read more about it in this answer: https://stackoverflow.com/a/2553892/11271671 – Laisender May 31 '22 at 13:36
  • 1
    @Laisender actually that advice is a long time out of date...mysqli will handle errors automatically in the newest versions, and even in slightly older ones you can just turn on its exception reporting mode, so you'll see all errors automatically without having to write mountains of repetitive boilerplate error-handling code after every sql command (which takes time to implement, and sits there cluttering up your flow and reducing readability). See [How to get the actual mysqli error](https://stackoverflow.com/a/22662582/5947043). – ADyson May 31 '22 at 13:52
  • @ADyson Well in this specific instance, the error was just echoing out on the screen, which is probably not what you want right? – Laisender May 31 '22 at 13:58
  • 2
    @Laisender your solution would also cause it to echo onto the screen...whereas if you enable mysqli exception reporting a) you get a more detailed sql-specific error message than the one the OP showed, and b) it becomes part of PHP's general error handling, and you can set config options for whether errors/warnings/notices are echoed or not, and whether they are logged to a file or not. So it's much more controllable. And it only requires 1 line of code to enable (or none, in PHP 8.1). See also https://phpdelusions.net/mysqli/error_reporting and https://php.watch/versions/8.1/mysqli-error-mode – ADyson May 31 '22 at 14:13

0 Answers0