PDO update does not replace placeholders in prepared statement

I am trying to update a table with the following code. If I change WHERE temp_booking_id = ':temp_booking_id'"); to use the actual current temp_id session, the query will be launched, but will add placeholders to the table (for example: statement) as a value.

$data contains the correct values, but does not replace placeholders.

He looked at it for hours and could not have my life determined what the problem was, and looked around, but could not find a solution.

PDOStatement:errorInfo() returns

PDOStatement :: errorInfo (): Array ([0] => 00000)

and if I remove the inverted commas around the placeholders, it returns

PDOStatement :: errorInfo (): Array ([0] => HY093)

Any ideas?

 try { $data = array( 'temp_booking_id' => $_SESSION['temp_id'], 'check_in' => $in, 'check_out' => $out, 'adults' => $a, 'children1' => $c1, 'children2' => $c2, 'infants' => $i, 'cots' => $c, 'promo_code' => $pc ); $STH = $DBH->prepare("UPDATE b_temp_booking SET check_in = ':check_in', check_out = ':check_out', adults = ':adults', children1 = ':children1', children2 = ':children2', infants = ':infants', cots = ':cots', promo_code = ':promo_code' WHERE temp_booking_id = ':temp_booking_id'"); $STH->execute($data); echo "\nPDOStatement::errorInfo():\n"; $arr = $STH->errorInfo(); print_r($arr); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } 
+4
source share
1 answer

Hmm, it looks like your SQL statement does not require single quotes. For example, you can try running this block:

  $STH = $DBH->prepare("UPDATE b_temp_booking SET check_in = :check_in, check_out = :check_out, adults = :adults, children1 = :children1, children2 = :children2, infants = :infants, cots = :cots, promo_code = :promo_code WHERE temp_booking_id = :temp_booking_id"); 

Check out the PHP manual in the prepared PDO reports: http://www.php.net/manual/en/pdo.prepared-statements.php It looks like quotes are not needed around named placeholders here.

Also, try running their example where they use the bindParam () method:

 $STH->bindParam(':temp_booking_id', $temp_booking_id); $temp_booking_id = $_SESSION['temp_id']; // Not sure how binding the environment variable will work, so decoupling it. $STH->bindParam(':check_in', $in); $STH->bindParam(':check_out', $out); $STH->bindParam(':adults', $a); $STH->bindParam(':children1', $c1); $STH->bindParam(':children2', $c2); $STH->bindParam(':infants', $i); $STH->bindParam(':cots', $c); $STH->bindParam(':promo_code', $pc); 

When you are ready to run, you can run the following line:

 $STH->execute(); 

Check this out and see if the parameter you are looking for is optional.

+4
source

All Articles