如何使用复选框在MySQL中进行更新

时间:2016-10-24 09:01:31

标签: javascript jquery

我想了解MySQLcheckbox的多次更新是多少,因为我没有获得有人可以帮助我的id属性checkbox?< / p>

HTML代码

<form method="post" action="../sys/fav.php">
        <input type="hidden" id="id_photo" name="id" value="">

          <div class="col-md-12">
            <p align="right"><button class="btn btn-primary" id="final" name="final">Send</button></p>
            </div>
          <?php 

          require "../sys/connect.php";
          $email = $_GET['email'];

          $sql = mysqli_query($mysqli, "SELECT * FROM gallery WHERE email ='$email'");
          while($row = mysqli_fetch_array($sql)){
            $id = $row['id'];
            $name = $row['nome_galeria'];
            $emailcontact = $row['email_contact'];
            $pass = $row['pass'];
            $email = $row['email'];
            $img = $row['img'];

            print"<div class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12\">
              <input type=\"hidden\" name=\"contact\" value=\"$emailcontact\">
              <input type=\"hidden\" name=\"login\" value=\"$email\">
              <input type=\"hidden\" name=\"pass\" value=\"$pass\">
              <input type=\"hidden\" name=\"name\" value=\"$name\">
              <div class=\"hovereffect\">
                  <img id=\"he\" class=\"img-responsive\" src=\"../images/images/gallery/big/$img\" alt=\"$name\">
                  <div class=\"overlay\">
                     <div class=\"btn-group\" data-toggle=\"buttons\">
                        <label class=\"btn btn-primary cke\">
                          <input type=\"checkbox\" name=\"ck[]\" class=\"ck\" value=\"not\" id=\"ck_$id\"><i class=\"fa fa-heart\"></i>
                        </label>
                     </div>
                  </div>
              </div>
          </div>";

          }

          mysqli_close($mysqli)
          ?>
          </form>

PHP代码

require "conexao.php";

    if(isset($_POST['final'])){
        $id = $_POST['id'];
    foreach($_POST['ck'] as $ck){
        $check = $ck;
        $sql = mysqli_query($mysqli,"UPDATE gallery SET fav = '$check' WHERE id = '$id'")or die(mysqli_error($mysqli));
    }

    if($sql)
          echo "Success!";
        else
          echo "Fail!";
   }

代码Js获取id

checkbox
$("#final").click(function(){
          var str = "";
            var boxes = $(".ck");
            for(var i = 0; i< boxes.length; i++){
              if(boxes[i].checked == true){
                var tmp = boxes[i].id.split("_");
                str+=(i ? "," : "")+tmp[1];
              }
            }
            document.getElementById('id_fotos').value=str;

        });

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取复选框的值,并通过ajax

var ckArray = new Array();
$.each($('input[name="ck[]"]:checked'), 
    function(index, ele){
        ckArray.push($(ele).val());
    });