0

This is the code which I'm trying to use for the query:

$data=[
  'e_id' => $eid,
  'e_name' => $ename,
  'e_deptid' => $edid,
  'e_sal' => $esal,
];

foreach($data as $keys=>$values){
  if($key==0){
    $fields .= $keys . "=?";
  } else {
    $fields .= ", " . $keys . "=?";
  }
  $key++;
}

$query = "INSERT INTO employee SET $fields";
$stmt = $conn->prepare($query);
$stmt->bind_param(
  "isii", $data['e_id'], $data['e_name'], 
   $data['e_deptid'], $data['e_sal']
);

I am getting the following error:

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\employee\employeesubmit.php:35 Stack trace: #0 {main} thrown in C:\xampp\htdocs\employee\employeesubmit.php on line 35.

what may be the reason for this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • It most probably means your query FAILED – RiggsFolly Sep 20 '18 at 10:15
  • 1
    I would try echo'ing `$query` and trying to run it via `phpMyAdmin` or something like that – RiggsFolly Sep 20 '18 at 10:15
  • 1
    **Or add some Error checking** but if you cannot be bothered, Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Sep 20 '18 at 10:16
  • but when i am using normal insert into without the SETkeyword its working fine – Janani Brindha Sep 20 '18 at 10:17
  • Doesn't INSERT INTO work with VALUES instead of SET? So INSERT INTO employee VALUES $fields – Wimanicesir Sep 20 '18 at 10:17
  • 1
    @Wimanicesir Strangely you can use `SET` in and INSERT, just that most people dont – RiggsFolly Sep 20 '18 at 10:18
  • Add the output from `echo $query;` to your question please – RiggsFolly Sep 20 '18 at 10:19
  • Error message says that `$conn->prepare($query);` statement returns Boolean value.Most probably `false`.Try `var_dump($stmt)` to check that.If that so then there is something wrong with your mysql connection or related code – Nuwan Attanayake Sep 20 '18 at 10:29
  • thanks to all issues cleared – Janani Brindha Sep 21 '18 at 11:10

0 Answers0