Php PDO插入错误:SQLSTATE[HY093]:参数编号无效:未定义参数

Php PDO插入错误:SQLSTATE[HY093]:参数编号无效:未定义参数,php,pdo,sql-insert,Php,Pdo,Sql Insert,在尝试执行PDO插入时,我无法找到错误的解决方案。我一直在犯错误 SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 这是我的密码: try{ $STH = $DBH->prepare("INSERT INTO members (fname, mname, lname, gender, dob, id, nation, mstatus, mobile, tel, address

在尝试执行PDO插入时,我无法找到错误的解决方案。我一直在犯错误

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
这是我的密码:

    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)");
这里是我添加bindValues的地方(我之所以这样做是因为StackOverflow不允许使用大的代码块)

$STH->bindValue(1,$\u 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,$u POST['residence],PDO::PARAM_STR);
$STH->bindValue(16,$_POST['sacco'],PDO::PARAM_STR);
$STH->bindValue(17,$\u POST['nk\u name',PDO::PARAM\u STR);
$STH->bindValue(18,$_POST['relationship'],PDO::PARAM_STR);
$STH->bindValue(19,$_POST['age'],PDO::PARAM_INT);
$STH->bindValue(20,$\u POST['nk\u id',PDO::PARAM\u STR);
$STH->bindValue(21,$\u POST['nk\u tel',PDO::PARAM\u STR);
$STH->bindValue(22,$\u POST['nk\u address'],PDO::PARAM\u STR);
$STH->bindValue(23,$_POST['photo'],PDO::PARAM_STR);
$STH->bindValue(24,$\u POST['idlink',PDO::PARAM_STR);
$STH->execute();
}
捕获(PDO$e){
echo“数据库错误:无法添加成员。
”$e->getMessage(); }捕获(例外$e){ echo“常规错误:无法添加成员。
”$e->getMessage(); }
有两种方法可以修复它们。。(选择任意一种情况,不能同时使用)

案例1:命名占位符 案例2:问号占位符
不能同时使用数字和名称,它们必须相同,因此将
1
替换为
:fname
$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();  
    }
$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..
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...)