PHP + SQL - 存在时选择大小写(...) - 如果0插入行

时间:2016-01-19 18:36:44

标签: php mysql sql if-statement post

使用SELECT CASE WHEN EXISTS()THAST CAST(1 AS BIT)等查询,然后根据该查询的结果请求插入,删除或不执行操作。代码按预期插入和删除;问题是它拒绝什么都不做,即使已经存在,也要添加一个新行。有人可以说明为什么会这样吗?以下代码:

'的java'是一个复选框的名称。 ' $用户'是包含$ _SESSION [" UserID"]的变量。

PHP:

if (isset($_POST['java'])){

                $sql = $con->query("SELECT CASE WHEN EXISTS 
                (SELECT * FROM userskills
                WHERE UserID = $User AND SkillID = 1)
                THEN CAST(1 AS BIT)
                ELSE CAST(0 AS BIT) END");

                if ($sql == "0"){

                $sql = $con->query("INSERT INTO userskills ( UserID, SkillID) VALUES  ($User, 1)");

            }} else{

                $sql = $con->query("SELECT CASE WHEN EXISTS 
                (SELECT * FROM userskills
                WHERE UserID = $User AND SkillID = 1)
                THEN CAST(1 AS BIT)
                ELSE CAST(0 AS BIT) END");

                if ($sql == "1"){

                $sql = $con->query("DELETE FROM userskills 
                WHERE UserID = $User AND SkillID = 1");

                }}

HTML:

<div class="RightBody">
    <form id="form1" name="form1" method="post" enctype="multipart/form-data">
      <div class="FormElement">
        <input name="FirstName" type="text" class="TField" id="FirstName" placeholder="First Name" value="<?php echo $_SESSION["FirstName"]; ?>">
      </div>
      <div class="FormElement">
        <input name="LastName" type="text" class="TField" id="LastName" placeholder="Last Name" value="<?php echo $_SESSION["LastName"]; ?>">
      </div>
      <div class="FormElement">
        <input name="Email" type="email" class="TField" id="Email" placeholder="Email Address" value="<?php echo $_SESSION["Email"]; ?>">
      </div>
      <div class="FormElement">
        <input name="JobRole" type="text" class="TField" id="JobRole" placeholder="Job Role" value="<?php echo $_SESSION["Role"]; ?>">
      </div>
      <div class="FormElement">
        <input name="Password" type="password" class="TField" id="Password" placeholder="Password" required="requried">
      </div>
      <div class="FormElement">
        <input type="file" name="file">
        <br>
        <br>
      </div>
      <p>
        <label>
          <input type="checkbox" name="java" value="checkbox" id="CheckboxGroup1_0">
          Java</label>
        <br>
        <label>
          <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_1">
          Checkbox</label>
        <br>
        <label>
          <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_2">
          Checkbox</label>
        <br>
      </p>
<div class="FormElement">
        <input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
      </div>
    </form>
  </div>

修改

当我运行时,我收到以下错误:

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便使用“BIT”ELSE CAST(0 AS BIT)END&#39;在第4行

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,我寻找其他解决方案来检查行(WHERE ...)是否存在,并报告为truefalse以便我可以继续到INSERTDELETE或不执行任何操作。

以下链接包含一个答案,使我能够获得理想的结果; Simple mysql Query to check if row exist

感谢所有试图帮助回答我的问题的人。 :)