0

Code

if(isset($_POST['create'])){
    $fname      = trim($_POST['fname']);
    $lname      = trim($_POST['lname']);
    $ftname         = trim($_POST['ftname']);
    $mtname         = trim($_POST['mtname']);
    $date_of_admission = trim($_POST['date_of_admission']);
    $date_of_birth = trim($_POST['date_of_birth']);
    $photo_location         = trim($_POST['photo_location']);
    $address        = trim($_POST['address']);
    $phone      = trim($_POST['phone']);
    $sex    = trim($_POST['sex']);
    $nationality    = trim($_POST['nationality']);
    $religion   = trim($_POST['religion']); 

    if(empty($fname) && empty($lname) &&empty($ftname) &&empty($mtname) &&empty($date_of_admission) && empty($phone) && empty($sex)){
        $error = "You must fill all fields.";
    }else{
        $insert = $db->prepare("INSERT INTO st_info (fname,  lname,  ftname,  mtname,  date_of_birth,  date_of_admission,  photo_location, address, phone, sex, nationality, religion,joined) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
        $insert->bind_param('sssssssssddd',$fname,$lname,$ftname,$mtname,$date_of_birth,$date_of_admission,$photo_location,$address,$phone,$sex,$nationality,$religion);
        if($insert->execute()){
            //$success = "st_info added successfully!";
            header("location:index.php");
        }
    }
}

Error

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\create.php on line 26

Peter
  • 1,646
  • 4
  • 26
  • 43
  • Not related to the problem, but your if-check is wrong. In order to check if all fields are filled, use the or operator (`||`) instead. The way you have it set now, if something is *not* empty, everything afterwards will not be checked anymore. This code now only throws the error if nothing is filled in at all, but once one field is filled, it will not complain anymore. – Oldskool Oct 28 '15 at 09:01

3 Answers3

0

Looks like $db->prepare() returns a boolean and not an object. Try this maybe?

if(isset($_POST['create'])){
    $fname      = trim($_POST['fname']);
    $lname      = trim($_POST['lname']);
    $ftname         = trim($_POST['ftname']);
    $mtname         = trim($_POST['mtname']);
    $date_of_admission = trim($_POST['date_of_admission']);
    $date_of_birth = trim($_POST['date_of_birth']);
    $photo_location         = trim($_POST['photo_location']);
    $address        = trim($_POST['address']);
    $phone      = trim($_POST['phone']);
    $sex    = trim($_POST['sex']);
    $nationality    = trim($_POST['nationality']);
    $religion   = trim($_POST['religion']);


    if(empty($fname) && empty($lname) &&empty($ftname) &&empty($mtname) &&empty($date_of_admission) && empty($phone) && empty($sex)){
        $error = "You must fill all fields.";
    }else{
        $db->prepare("INSERT INTO st_info (fname,  lname,  ftname,  mtname,  date_of_birth,  date_of_admission,  photo_location, address, phone, sex, nationality, religion,joined) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
        $db->bind_param('sssssssssddd',$fname,$lname,$ftname,$mtname,$date_of_birth,$date_of_admission,$photo_location,$address,$phone,$sex,$nationality,$religion);
        if($db->execute()){
            //$success = "st_info added successfully!";
            header("location:index.php");
        }

    }
}
Christian
  • 1,515
  • 10
  • 16
0

It seems your prepare statement fails (Otherwise it would return a PDOStatement and not a boolean)

You should use $db->errorInfo() and $db->errorCode() to get the reason for your error.

A reason could be, you have some fields in your statement, which doesn't exists, or you named them wrong.

Philipp
  • 15,257
  • 4
  • 29
  • 47
0

one of most common failures at prepared statements is connection encoding.. try

$mysqli->set_charset("utf8");

after

$mysqli = new mysqli("host", "user", "pass", "db"); 
freeflight
  • 19
  • 4