1

I try to bind parameters to sql (mysqli) in php, but there is an error code as written above. This is the code I wrote:

$stmt = $conn->prepare("INSERT INTO assignments (Classes_has_Subjects_classesHasSubjectsId, assignmentName, gradeForAssignment,protectiveGrade) 
                                  VALUES (?, ?, ?, ?)");
if ($stmt = false)
{
    echo "false";
}
else{
    $stmt->bind_param("sss", $classesHasSubjectsId, $assignmentName, $gradeForAssignment,  $protectiveGrade);
    $stmt->execute();
}

and here is the error message: 11

Fatal error: Call to a member function bind_param() on boolean in

What's wrong?

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
RD7
  • 628
  • 4
  • 9
  • 20
  • missing one `s` OR `i` here `$stmt->bind_param("sss"` – Saty May 02 '16 at 12:21
  • `if ($stmt = false)` You're always setting `$stmt` on `false` here. – Daan May 02 '16 at 12:22
  • 1
    some teacher sad to write value before variable, like `if (false == $stmt)`, in comparisons so you will avoid those kind of errors – fiorebat May 02 '16 at 12:25
  • 1
    Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Nov 11 '19 at 21:51
  • I found myself searching for "bool" type on "bind_param" and the associated similar question has nothing in common. I vote to re-open. Please, remember that people looking for an answer search for the question, not by the answer they don't already know, in consequence, even if the related question eventually bring light on the topic, it is not a duplicated. – Adrian Maire Jul 09 '20 at 19:25

1 Answers1

5
<?php
$stmt = $conn->prepare("INSERT INTO assignments (Classes_has_Subjects_classesHasSubjectsId, assignmentName, gradeForAssignment,protectiveGrade) 
                                  VALUES (?, ?, ?, ?)");
    if (!$stmt) {
        echo "false";
    }
    else {
        $stmt->bind_param("ssss", $classesHasSubjectsId, $assignmentName, $gradeForAssignment,  $protectiveGrade);
        $stmt->execute();
    }

if ($stmt = false) means you're assigning false to $stmt. Replace it with if (!$stmt).

You're binding 4 parameters but only providing 3 types of values. I added a s in the bind_param function, assuming all your values are string. If some of the values are integers replace the corresponding s with an i. If there are doubles, replace with a d.

Are you sure the first field's name in the assignments table is Classes_has_Subjects_classesHasSubjectsId?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
D14n4
  • 130
  • 6