如何将条件语句传递给MySQL查询 [英] How to pass condition statements to a mySql query

查看:78
本文介绍了如何将条件语句传递给MySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新此问题,以更好地阐明所要查找的内容.我将数组传递给应该更新购物车的函数.我有两个变量,$isbn$formatQuantity.

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. 检查$isbn是否存在,是否只是更新$formatQuantity,是否不创建以$isbn作为主键的列

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

$formatQuantity可能是精装书,精装书或电子书...我需要更新右列的数量,因此,如果格式是平装书,我需要在列平装书购买的当前值上加1.

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

这是我的代码(请问我知道如何做所有其他事情,但是我不知道如何使用MySQL检查条件):

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";
}
}

 ?>

推荐答案

如果我收到您的问题,则可以使用split()函数,该函数将根据字符串中模式的出现将字符串分为各种元素. 并在拆分字符串后返回字符串数组.

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.

这篇关于如何将条件语句传递给MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆