Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

php - How to pass condition statements to a mySql query

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

  1. check if $isbn exists, if it does just update $formatQuantity, if it does not create a column with $isbn as primary key

  2. $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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If i get your question, you could use split() function it will divide a string into various element based on the occurrence of pattern in string. and returns an array of strings after splitting up a string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...