PDO Insert Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

StackOverflow https://stackoverflow.com/questions/23240292

  •  08-07-2023
  •  | 
  •  

Domanda

I haven't been able to find a solution to the error I'm receiving when trying to do a PDO insert. I keep getting the error

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Here is my code:

    try{
        $STH = $DBH->prepare("INSERT INTO members (fname, mname, lname, gender, dob, id, nation, 
mstatus, mobile, tel, address, county, email, o_email, residence, sacco, nk_name, relationship, age, 
nk_id, nk_tel, nk_address, photo, idlink) VALUES (:fname, :mname, :lname, :gender, :dob, :id, :nation, 
:mstatus, :mobile, :tel, :address, :county, :email, :o_email, :residence, :sacco, :nk_name, :relationship, :age, 
:nk_id, :nk_tel, :nk_address, :photo, :idlink)");

Here is where I add bindValues (I'm only doing this because StackOverflow doesn't allow large codeblocks)

$STH->bindValue(1, $_POST['fname'], PDO::PARAM_STR);
$STH->bindValue(2, $_POST['mname'], PDO::PARAM_STR);
$STH->bindValue(3, $_POST['lname'], PDO::PARAM_STR);
$STH->bindValue(4, $_POST['gender'], PDO::PARAM_STR);
$STH->bindValue(5, $_POST['dob'], PDO::PARAM_STR); 
$STH->bindValue(6, $_POST['id'], PDO::PARAM_STR);
$STH->bindValue(7, $_POST['nation'], PDO::PARAM_STR);
$STH->bindValue(8, $_POST['mstatus'], PDO::PARAM_STR); 
$STH->bindValue(9, $_POST['mobile'], PDO::PARAM_STR);
$STH->bindValue(10, $_POST['tel'], PDO::PARAM_STR);
$STH->bindValue(11, $_POST['address'], PDO::PARAM_STR);
$STH->bindValue(12, $_POST['county'], PDO::PARAM_STR);
$STH->bindValue(13, $_POST['email'], PDO::PARAM_STR);
$STH->bindValue(14, $_POST['o_email'], PDO::PARAM_STR);
$STH->bindValue(15, $_POST['residence'], PDO::PARAM_STR);
$STH->bindValue(16, $_POST['sacco'], PDO::PARAM_STR);
$STH->bindValue(17, $_POST['nk_name'], PDO::PARAM_STR); 
$STH->bindValue(18, $_POST['relationship'], PDO::PARAM_STR);
$STH->bindValue(19, $_POST['age'], PDO::PARAM_INT);
$STH->bindValue(20, $_POST['nk_id'], PDO::PARAM_STR);
$STH->bindValue(21, $_POST['nk_tel'], PDO::PARAM_STR);
$STH->bindValue(22, $_POST['nk_address'], PDO::PARAM_STR);
$STH->bindValue(23, $_POST['photo'], PDO::PARAM_STR);
$STH->bindValue(24, $_POST['idlink'], PDO::PARAM_STR);

$STH->execute();

}
    catch (PDOException $e) {
    echo "DataBase Error: The member could not be added.<br>".$e->getMessage();
    } catch (Exception $e) {
        echo "General Error: The member could not be added.<br>".$e->getMessage();  
    }
È stato utile?

Soluzione

There are two ways to fix them up.. (Choose any one case , you can't use both together)

Case 1: Named placeholders

$STH->bindValue(':fname', $_POST['fname'], PDO::PARAM_STR);
$STH->bindValue(':mname', $_POST['mname'], PDO::PARAM_STR);
$STH->bindValue(':lname', $_POST['lname'], PDO::PARAM_STR);
//...

//.. so on..

Case 2: Question mark placeholders

try{
        $STH = $DBH->prepare("INSERT INTO members (fname, mname, lname, gender, dob, id, nation, 
mstatus, mobile, tel, address, county, email, o_email, residence, sacco, nk_name, relationship, age, 
nk_id, nk_tel, nk_address, photo, idlink) VALUES (?,?,?,?,?,

 // .. so on..  (note the ? symbols...)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top