1

Ive been starting at the computer screen for hours (doesn't help that Adobe Dreamweaver has a white background)

Question

I can't find this error to save my life, would greatly appreciate it if someone could give this a scan and tell me where i am going wrong!

Advice Needed

Im new to PDO and just made the switch i'm slowly getting the hang of it but i'm battling with debugging, when using mysql queries with PDO in php. It was rather easy to find the query errors on the old mysql_* method but as a PDO novice i'm having a hard time debugging. What would be the closes PDO alternative to mysql_error()? Any help and advice appreciated.

Codde Below (please keep in mind i'm a novice so code might not be the prettiest)

$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();

Error

: Invalid parameter number: parameter was not defined

Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97
  • I've seen the "rare" case where when leaving out the colon `('carType', $carType)` would cause that. So try adding it `(':carType', $carType)`. Edit: I'm wrong here, after seeing @gabe3886 's comment, but my comment does hold "some truth". – Funk Forty Niner Nov 13 '16 at 15:58
  • 1
    The query has `:car_group` but you bind `carType`. – gabe3886 Nov 13 '16 at 15:58
  • You have not defined a placeholder called `carType` in your sql query, but you still try to bind a value to it. – Decent Dabbler Nov 13 '16 at 15:59

1 Answers1

0

you have some incoherencies when you bind your parameters. It should be something like :

$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);

Also, $resId should be protected too, see on my snippet the usage of PDO::PARAM_INT constant, because you are using an integer and it should not be quoted.

Max
  • 891
  • 8
  • 17
  • Why have you set the pickup_time to be `$statement->bindValue(':pickup_time', $resId, PDO::PARAM_INT);`? There's no need for that to be set that way. Edit: And now your edit mentions parametising resID, but doesn't set it – gabe3886 Nov 13 '16 at 16:00
  • Thanks @gabe3886, updated answer – Max Nov 13 '16 at 16:05