如果选中则如何更新数据库1的复选框值,如果未选中则更新为0?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果选中则如何更新数据库1的复选框值,如果未选中则更新为0?相关的知识,希望对你有一定的参考价值。

This is my database

<?php
if(isset($_POST["insert"]))
{
   $conn = mysqli_connect("localhost", "root", "", "databaseappfeature");

   if(isset($_POST["insert"]) == "1"){
   $query = "UPDATE appfeature SET feature_switch = ('".$_POST["insert"]."')";
   $result = mysqli_query($conn, $query);
   echo "Data Inserted Successfully!";


  }
}
?>

This is my javascript code

<script>
  $(document).ready(function(){
     $('#submit').click(function(){
       var insert = [];

       $('.get_value').each(function(){
         if($(this).is(":checked"))
         {
         insert.push($(this).val());
         }
       });

       insert = insert.toString();

       $.ajax({
       url: "insert.php",
       method: "POST",
       data:{insert:insert},
       success:function(data){
       $('#result').html(data);
       }
       });
     });
  });
</script>
This is my checkbox code

<form action="" method="POST">

<h4 id="result"></h4>

<div class="container">
  <h2 align="center">Table App Feature</h2>   
        
  <table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Please check to enable the features</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Smarthome</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Intercom</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>

       <tr>
        <td>Visitors</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Booking</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Access</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Parcel</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Bills</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Invoices</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      <tr>
        <td>Receipts</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Transactions</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Meeting</td>
        <td>
          <input type="checkbox" class="get_value" value="1"  />
        </td>
      </tr>
      <tr>
        <td>Vote</td>
        <td>
          <input type="checkbox" class="get_value" value="1"/>
        </td>
      </tr>
      <tr>
        <td>Feedback</td>
        <td>
          <input type="checkbox" class="get_value" value="1" />
        </td>
      </tr> 
    </tbody>
  </table><br />
  <div align="center">
    <button type="button" name="submit" id="submit">Update</button>
  </div>
</div>
</form>

如何将多个复选框的值更新到数据库中,如果选中该复选框,则值应为1;如果未选中该复选框,则值应为0? This is my checkboxThis is jquery that i use to pass the value of checkboxThis is my database code请帮助我..我对此很新......我这样做了一个星期..

答案

请查看文档并了解表单的工作方式。有很多例子我会推荐Head First书系列和here你可以找到一个很好的例子。

此处还有您问题的示例代码

创建一个名为example.html的文件并保存此内容

    <html>
    <head>
<!-- link to jquery lib -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    <form action="" method="POST">

    <h4 id="result"></h4>

    <div class="container">
      <h2 align="center">Table App Feature</h2>   
            <label> Name </label>
        <input type='text' name='name' id='name' value=''/>
      <table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
        <thead>
          <tr>
            <th>Firstname</th>
            <th>Please check to enable the features</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Smarthome</td>
            <td>
<!-- checkbox for smarthome value -->
              <input type="checkbox" class="get_value" id='smarthome'/>
            </td>
          </tr>
        <tr>
            <td>Intercom</td>
            <td>
              <input type="checkbox" class="get_value" id='intercom'/>
            </td>
          </tr>
        </tbody>
      </table><br />
      <div align="center">
        <label>check if you want to update, unckeck if you want to insert</label>
        <input type="checkbox" class="get_value" id='update'/>
        <br>
<!-- button name -->
            <button type="button" name="submit" id="submit">Update or Insert</button>
      </div>
    </div>
    </form>
    </body>
    <script type="text/javascript">
      $(document).ready(function(){
         $('#submit').click(function(){
// get the value is checked from the form
           $.ajax({
           url: "insert.php",
           method: "POST",
           data:{intercom: $('#intercom').is(':checked'),smarthome: $('#smarthome').is(':checked'), name: $('#name').val(), update: $('#update').is(':checked')},
           success:function(data){
           $('#result').html(data);
           }
           });
         });
      });
    </script>
    </html>

php文件如下命名为insert.php。确保两个文件都位于同一目录和Apache服务器内。(localhost public directory)

<?php
$servername = "localhost";
$username = "YOURDBUSER";
$password = "YOURDBPASSWORD";
$dbname = "databaseappfeature"; // db name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql ;

if($_POST['update']){
    $sql = "UPDATE appfeature SET smarthome=".$_POST['smarthome'].", intercom=".$_POST['intercom']." WHERE name='".$_POST['name']."'";
}else{
    $sql = "INSERT INTO appfeature (name, smarthome, intercom) VALUES ('".$_POST['name']."',".$_POST['smarthome'].",".$_POST['intercom'].")";
}

if ($conn->query($sql) === TRUE && !$_POST['update']) {
    echo "New record created successfully";
}else if($conn->query($sql) === TRUE && $_POST['update']){
    echo "record updated";
}else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

here是与我使用的数据库相关的sql文件

以上是关于如果选中则如何更新数据库1的复选框值,如果未选中则更新为0?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 if 语句中显示/比较 mysql 行的动态值?

如果选中,则反应复选框将值添加到数组

如果提交表单时未选中复选框,如何将布尔值更新为 0

如果所有复选框都未选中,则禁用按钮,如果至少选中一个,则启用它

如果在页面初始加载时未选中复选框,则返回无效提交

如果在 Woocommerce 中未选中复选框,则隐藏元素