简体   繁体   中英

Why I'm getting this error "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined"?

I've got this code to insert data to MySQL. The error occur when I try to call the function register_emp(). Can you please tell me what to do in order to fix this kind of error. Thank you.

if($user->register_emp($last,$first,$middle,$middle,
                        $address,$contact,$email,$birth,$gender,
                        $status,$citizen,$position,$dependent))
{
    try
    {

        $stmt = $user->runQuery("SELECT MAX(ID) as LastID from employeeprofile");
        $stmt->execute(array());
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
        if($stmt->rowCount() == 1)if($stmt->rowCount() == 1){
        $owner=$userRow['LastID'];

        if($user->register_contrib($owner,$sss,$tin,$pagibig,$phil)){
            $user->redirect('admin1.php');
        }
    }

}
catch(PDOException $e)
{
    echo $e->getMessage();
}

}

This is the register_emp function that I'm trying to call.

public function register_emp($last,$first,$middle,$address,
                            $zip,$contact,$email,$birth,
                            $gender,$status,$citizen,$position,$dependent)
{
    try
    {


        $stmt = $this->conn->prepare("INSERT INTO `proll`.`employeeprofile` 
        (`Lastname`, `Firstname`, `Middlename`, `Address`, `ZIP`, `Contact`, `Email`, `Birthdate`, `Gender`, `Status`, `Citizenship`, `Position`, `Dependent`) VALUES 
        (:last,:first,:middle,:address,:zip,:contact,:email,:birth,:gender,:status,:citizenship,:position,:dependent)");


        $stmt->bindparam(":last", $last);
        $stmt->bindparam(":first", $first);
        $stmt->bindparam(":middle", $middle);
        $stmt->bindparam(":address", $address);
        $stmt->bindparam(":zip", $zip);
        $stmt->bindparam(":contact", $contact);
        $stmt->bindparam(":email", $email);
        $stmt->bindparam(":birth", $birth);
        $stmt->bindparam(":gender", $gender);
        $stmt->bindparam(":status", $status);       
        $stmt->bindparam(":citizen", $citizen); 
        $stmt->bindparam(":position", $position);
        $stmt->bindparam(":dependent", $dependent);


        $stmt->execute();   

        return $stmt;   
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }               
}

The issue is with citizen/citizenship. You're using different words for the prepare statement and binding:

$stmt = $this->conn->prepare("INSERT INTO `proll`.`employeeprofile` 
    (`Lastname`, `Firstname`, `Middlename`, `Address`, `ZIP`, `Contact`, `Email`, `Birthdate`, `Gender`, `Status`, `Citizenship`, `Position`, `Dependent`) VALUES 
    (:last,:first,:middle,:address,:zip,:contact,:email,:birth,:gender,:status,:citizenship,:position,:dependent)");

....

    $stmt->bindparam(":citizen", $citizen);

Change them to be the same word, and you'll be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM