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

时间:2018-06-11 05:56:11

标签: javascript arrays database checkbox



cat file.csv | tr -d " \t\n\r" | tr '|' '\n' |sed "s/.$//" > filetemp.csv && cp filetemp.csv file.csv






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


  }
}
?>
&#13;
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>
&#13;
&#13;
&#13;

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

1 个答案:

答案 0 :(得分:0)

请浏览文档并了解表单的工作方式。有很多例子我会推荐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公共目录)

<?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文件