I am updating this question to better clarify what am looking for. I am passing an array to a function which is supposed to update a cart. I have two variables, $isbn
and $formatQuantity
.
Here is my problem before updating the database I need to check a few condition
check if $isbn
exists, if it does just update $formatQuantity
, if it does not create a column with $isbn
as primary key
$formatQuantity
could be softcover, hardcover, or ebook... I need to update the quantity of the right column so if format is softcover I need to add 1 to the current value of column softcover purchase..
Here is my code (P.s I know how to do everything else but I don't know how to check the condition with MySQL):
<?php
function insertBook($db,$selection){
$isbn;
$format;
foreach ($selection as $key => $value) {
$isbn=$key;
$format=$value;
$change= explode(":", $format) ;
$format=$change['0'];
}
$query = "INSERT INTO cart (isbn, hardcover_purchased, softcover_purchased, ebook_purchased)
VALUES (':isbn', ':format', 0, 0)";
$statement = $db->prepare($query);
$success = $statement->execute();
$statement->bindValue(':isbn', $isbn);
$statement->bindValue(':format', $format);
$statement->closeCursor();
if ($success) {
echo "section inserted using query insertNewSection_checkSuccess";
}
else{
echo "Unable to insert new section using query insertNewSection_checkSuccess";
}
}
?>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…