简体   繁体   中英

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. 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. '$User' is the variable containing the $_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>

Edit:

When I run this, I receive the following error:

You have an error in your SQL syntax; 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

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.

The following link contains an answer that enabled me to get my desired outcome working; Simple mysql Query to check if row exist

Thanks to all who tried to help answer my question. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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