PDO参数数量无效b

时间:2016-11-13 15:54:48

标签: php mysql pdo

我已经在电脑屏幕上开了几个小时(没有帮助Adobe Dreamweaver有白色背景)

问题

我找不到这个错误来挽救我的生命,会非常感激,如果有人可以给你扫描并告诉我哪里出错了!

需要建议

我是PDO的新手,只是做了开关我慢慢得到它的悬念,但我正在与调试,在php中使用PDO的mysql查询。在旧mysql_*方法上查找查询错误相当容易,但作为PDO新手,我很难调试。什么是关闭PDO替代mysql_error()?任何帮助和建议表示赞赏。

Codde (请记住我是新手,所以代码可能不是最漂亮的)

$resId = $_POST['resId'];
$name = $_POST['name'];
$surname = $_POST['surname'];                
$coverName = $_POST['coverName'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$carType = $_POST['carGroup'];
$pickupDate = $_POST['pickupDate'];
$pickupTime = $_POST['pickupTime'];          
$returnDate = $_POST['returnDate'];
$returnTime = $_POST['returnTime'];
$rentalPeriode = $interval->format('%d'); //calculates correct


 $sql = 'UPDATE car_reservations SET 
               renter_name = :renter_name, 
               renter_lastname = :renter_lastname, 
               email = :email, 
               phone = :phone, 
               pickup_date = :pickup_date, 
               return_date = :return_date, 
               number_days = :number_days, 
               insurance = :insurance, 
               car_group = :car_group, 
               return_time = :return_time, 
               pickup_time = :pickup_time 
        WHERE res_id = $resId';

$statement = $db->prepare($sql);
$statement->bindValue(':renter_name', $name);
$statement->bindValue('renter_lastname', $surname);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':pickup_date', $pickupDate);
$statement->bindValue(':return_date', $returnDate);
$statement->bindValue(':number_days', $rentalPeriode);
$statement->bindValue(':insurance', $coverName);
$statement->bindValue('carType', $carType);
$statement->bindValue(':return_time', $returnTime );
$statement->bindValue(':pickup_time', $pickupTime );


$success = $statement->execute();

错误

: Invalid parameter number: parameter was not defined

1 个答案:

答案 0 :(得分:0)

绑定参数时有一些不一致之处。 它应该是这样的:

$sql = 'UPDATE car_reservations SET 
renter_name = :renter_name, 
renter_lastname = :renter_lastname, 
email = :email, 
phone = :phone, 
pickup_date = :pickup_date, 
return_date = :return_date, 
number_days = :number_days, 
insurance = :insurance, 
car_group = :car_group, 
return_time = :return_time, 
pickup_time = :pickup_time 
WHERE res_id = :resId';

$statement = $db->prepare($sql);
$statement->bindValue(':renter_name', $name);
$statement->bindValue(':renter_lastname', $surname);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':pickup_date', $pickupDate);
$statement->bindValue(':return_date', $returnDate);
$statement->bindValue(':number_days', $rentalPeriode);
$statement->bindValue(':insurance', $coverName);
$statement->bindValue(':car_group', $carType);
$statement->bindValue(':return_time', $returnTime );
$statement->bindValue(':pickup_time', $pickupTime );
$statement->bindValue(':resId', $resId, PDO::PARAM_INT);

另外,$resId也应受到保护,请在我的代码段中查看PDO::PARAM_INT常量的用法,因为您使用的是整数且不应引用它。