I have a query that updates a boolean value for a given record, then inserts that record into another table (depending on the action performed) using a select subquery. The select subquery is working fine, but how can I add an additional variable on to the end of the query to be inserted ? The reason why I need to do this is because the select subquery doesn’t contain this data, and this data will only be stored in one of the two tables that the original record gets “copied” to.
$id = $_POST['id'];
$action = $_POST['action'];
$reason = $_POST['reason'];
if($action == "approve") {
$statement = $db->prepare("UPDATE waiting SET wait = :status WHERE id = :id");
$statement->bindValue(':status', 0);
$statement->bindParam(':id', $id);
$statement->execute();
$statement = $db->prepare("INSERT INTO approved (fname, lname, student_id, email, type) SELECT fname, lname, student_id, email, type FROM waiting WHERE id = :id");
$statement->bindParam(':id', $id);
$statement->execute();
$lastId = $db->lastInsertId();
$statement = $db->prepare("UPDATE approved SET reason = :reason WHERE id = $lastId");
$statement->bindParam(':reason', $reason);
$statement->execute();
}
Basically I want to combine the insert query that uses a select subquery and the update query that updates the reason into one query. Is this syntactically possible?
EDIT: I should be more clear and state that the reason being passed is a string entered by a user into a text field. It’s not a column or any other data in a table.
Thanks
You can always select a constant, and the
reasonvalue is just that from the SQL viewpoint, so you can write the insert as:Just bind the params then and run the query, and that’s it.