Delete row from table where A=x AND B=y using PHP pdo

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Delete row from table where A=x AND B=y using PHP pdo



I'm trying to delete from a database table where two fields match a value.
on the page javascript makes an ajax call to the php file below passing the relevant values, in the response however it returns "[object object]" rather than "deleted" and the record is still in the table.



Here's my code in PHP


if(isset($_POST['booking'])==true){
$booking = $_POST['booking'];
$vec_id= $_POST['id'];

$stmt = $pdo->prepare("DELETE FROM booked_vec WHERE booking_id = ? AND vec_id = ?");
$stmt->execute(array($booking,$vec_id));
$stmt = null;
}



It gets inside the if without problems. $booking and $vec_id both contain the correct values.



$pdo is a working connection to the database that works with other statements. I assume something wrong with either the statement or itself or the way I'm passing variables into the statement. I'm unable to find an example online of an AND being used in the context of a DELETE statement with pdo.



Could someone please advise where I'm going wrong?





php.net/manual/en/function.error-reporting.php and php.net/manual/en/pdo.error-handling.php then tell us what those errors were, if any.
– Funk Forty Niner
38 mins ago







if(isset($_POST['booking'])==true) that is a false positive. It needs to be broken up into 2 separate statements. Edit: "It gets inside the if without problems." - that's because it's always considered as being "true" and "set".
– Funk Forty Niner
37 mins ago




if(isset($_POST['booking'])==true)





@FunkFortyNiner There is not any syntax error.
– Saral
32 mins ago





@Saral oh? what makes you say that?
– Funk Forty Niner
32 mins ago





well there you go; submit your answer if you feel so confident about it :-) @Saral Edit: and see what others think about it.
– Funk Forty Niner
30 mins ago






1 Answer
1



Do it like this using bindParam() so it will bind $booking to the first placeholder and $vec_id to the second placeholder


$stmt = $pdo->prepare("DELETE FROM booked_vec WHERE booking_id = ? AND vec_id = ?");
$stmt->bindParam(1, $booking);
$stmt->bindParam(2, $vec_id);
$stmt->execute();



And also note that some drivers required to use closeCursor before executes another statement



PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a state that enables it to be executed again





Why? This improves nothing.
– Jay Blanchard
28 mins ago





It doesn't matter if it is working or not, it matters if it answers* the OP's question. This does not answer the question, only changes the method without an explanation of why the method should be changed.
– Jay Blanchard
26 mins ago





? is an anonymous placeholder. It is valid. The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed. php.net/manual/en/pdo.prepare.php
– user3783243
25 mins ago




?


The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed.





You don't need bindparam the execute function accepts parameters as they have it, execute(array($booking,$vec_id)).
– user3783243
22 mins ago


bindparam


execute


execute(array($booking,$vec_id))





Your code is (functionally) the exact same as the code the OP has.
– user3783243
20 mins ago








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived