简体   繁体   English

PHP + SQL-选择存在时的大小写(…)-如果为0,则插入行

[英]PHP + SQL - Select case when exists (…) - if 0 Insert row

Using the SELECT CASE WHEN EXISTS () THEN CAST (1 AS BIT) etc query, then based on the result of that query requesting the insert, deletion, or no action. 使用SELECT CASE WHEN EXISTS()然后CAST(1 AS BIT)等查询,然后根据该查询的结果请求插入,删除或不执行任何操作。 The code inserts and deletes as expected; 该代码按预期插入和删除; the problem is it refuses to do nothing, adding a new row even if one already exists. 问题是它拒绝执行任何操作,即使已经存在也添加了新行。 Can someone suggest the reason why this is happening? 有人可以指出发生这种情况的原因吗? The code below: 下面的代码:

'java' is the name of a tick box. “ java”是复选框的名称。 '$User' is the variable containing the $_SESSION["UserID"]. “ $ User”是包含$ _SESSION [“ UserID”]的变量。

PHP: 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: 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>

Edit: 编辑:

When I run this, I receive the following error: 运行此命令时,出现以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BIT) ELSE CAST(0 AS BIT) END' at line 4 检查与您的MySQL服务器版本相对应的手册,以在第4行的'BIT)ELSE CAST(0 AS BIT)END'附近使用正确的语法

In order to fix this problem I looked for other solutions to check if a row ( WHERE ...) exists, and report as true or false so I can go on to INSERT or DELETE or perform no action. 为了解决此问题,我寻求其他解决方案来检查是否存在行( WHERE ...),并报告为truefalse因此我可以继续执行INSERTDELETE或不执行任何操作。

The following link contains an answer that enabled me to get my desired outcome working; 以下链接包含一个答案,使我能够实现预期的效果; Simple mysql Query to check if row exist 简单的mysql查询来检查行是否存在

Thanks to all who tried to help answer my question. 感谢所有试图帮助回答我的问题的人。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM