PHP致命错误:未捕获异常'PDOException',消息'SQLSTATE [HY093]:参数号无效:参数未定义'

时间:2013-11-21 18:25:27

标签: pdo

我一直在尝试在过去4小时内调试以下PDO语句而没有任何结果..

<?php
//Update (Edit) user details in DB
// DB Connection include
include_once('connstring.inc.php'); 
//include_once('key.inc.php'); //mot necessary for update as UID already generated
$User_ID = $_POST['user_id'];
$First_Name = ucfirst($_POST['first_name']);
$Last_Name = ucfirst($_POST['last_name']);
$Address = $_POST['address'];
$Phone = $_POST['phone'];
$District = ucfirst($_POST['district']);
$State = ucfirst($_POST['state']);
$Country = ucfirst($_POST['country']);
$Pin_Code = $_POST['pin_code'];
$Email = $_POST['email'];
$User_Name = $_POST['user_name'];
//$UID = $_POST['UID'];
$Admin_Group = $_POST['admin_group'];
$Admin_Status = $_POST['admin_status'];
//update DB with new values
$sql = "UPDATE admin_user SET first_name = :firstname, last_name = :lastname, address=  :address, phone = :phone, district = :district, state = :state, country =     :country,pin_code = :pincode, email = :email, user_name = :username, admin_group =    :admingroup, admin_status = :adminstatus WHERE user_id = :userid";
 $stmt = $conn->prepare($sql);
$stmt->execute(array(':firstname'=>$First_Name, ':lastname'=>$Last_Name,  ':address'=>$Address, ':phone'=>$Phone, ':district'=>$District,':state'=>$State,':country'=>$Country,':pincode'=>$Pin_Code,':email' >$Email,':username'=>$User_Name,':admingroup'=>$Admin_Group,':admin_status'=>$Admin_Status, :userid'=>$User_ID));
  $conn = null;
 //Return result to jTable
  $jTableResult = array();
  $jTableResult['Result'] = "OK";
   print json_encode($jTableResult);
   ?>


My Mysql Table structure is as follows:
======================================
    (DB Name:admin_user)

    user_id
    first_name
    last_name
    address
    phone
    district
     state
    country
     pin_code
    email
    user_name
    password
    UID
    admin_group
    admin_status

我正在使用当前版本的jquery jtable插件。插入在同一个表中正常工作。但是在试图更新时我得到了:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]:  Invalid parameter number: parameter was not defined' in /var/www/test.com/public_html/admin/adminedituser.php:28\nStack trace:\n#0 /var/www/test.com/public_html/admin/adminedituser.php(28): PDOStatement->execute(Array)\n#1 {main}\n  thrown in /var/www/test.com/public_html/admin/adminedituser.php on line 28

我无法找到错误的原因,即使经过多次尝试..请帮助我......提前致谢。

1 个答案:

答案 0 :(得分:-1)

<?php
    /* 
     * Update (Edit) user details in DB
     * DB Connection include
     */
    include_once('connstring.inc.php'); 

    $user_info_arr = [];
    foreach($_POST as $key => $val)
    {
        $user_info_arr[$key] = $val;
    }

    //update DB with new values
    $query = "UPDATE `admin_user` SET 
                                  `first_name`      = :firstname, 
                                  `last_name`       = :lastname, 
                                  `address`         = :address, 
                                  `phone`           = :phone, 
                                  `district`        = :district, 
                                  `state`           = :state, 
                                  `country`         = :country,
                                  `pin_code`        = :pincode, 
                                  `email`           = :email, 
                                  `user_name`       = :username, 
                                  `admin_group`     = :admingroup, 
                                  `admin_status`    = :adminstatus 
                              WHERE 
                                  `user_id` = :userid";

    $stmt = $conn->prepare($query);
    $stmt->execute(
            array(
                ':firstname'    =>  ucfirst($user_info_arr['first_name']), 
                ':lastname'     =>  ucfirst($user_info_arr['last_name']),  
                ':address'      =>  $user_info_arr['address'], 
                ':phone'        =>  $user_info_arr['phone'], 
                ':district'     =>  ucfirst($user_info_arr['district']),
                ':state'        =>  ucfirst($user_info_arr['state']),
                ':country'      =>  ucfirst($user_info_arr['country']),
                ':pincode'      =>  $user_info_arr['pin_code'],
                ':email'        =>  $user_info_arr['email'],
                ':username'     =>  $user_info_arr['user_name'],
                ':admingroup'   =>  $user_info_arr['admin_group'],
                ':adminstatus'  =>  $user_info_arr['admin_status'], 
                ':userid'       =>  $user_info_arr['user_id']
            )
    );

    $conn = null;

    //Return result to jTable
    $jTableResult = [];
    $jTableResult['Result'] = "OK";
    print json_encode($jTableResult);
?>

它还需要我清理你的代码,并且真正使用$ _POST并传递给单个变量非常难看,而是使用数组......

尝试检查它现在是否有效