how to do an update on the bank using checkbox

0

I would like to know how I can do an update with checkbox , with the system I developed I can get normal all checkbox that will be changed but I can not get the id from them how can I do this? below is the part of html and that of php

HTML code

       while($aux = mysqli_fetch_array($sql)){
        $id = $aux['id'];
        $nome = $aux['nome'];
        $img = $aux['imgp'];

        print"<div class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12\">
        <input type=\"hidden\" name=\"id\" value=\"$id\">
          <div class=\"hovereffect\">
            <span class=\"abcd\"></span>
              <img id=\"he\" class=\"img-responsive\" src=\"../images/imagens/galeria/big/$img\" alt=\"$nome\">
              <div class=\"overlay\">
                 <div class=\"btn-group\" data-toggle=\"buttons\">
                    <label class=\"btn btn-primary cke\">
                      <input type=\"checkbox\" name=\"ck[]\" value=\"nao\" id=\"$id\"><i class=\"fa fa-heart\"></i>
                    </label>
                 </div>
              </div>
          </div>
      </div>";

      }

PHP code

require "conexao.php";

    if(isset($_POST["final"])){


    foreach($_POST['ck'] as $ck){
        $check = $ck;
        $id = $_POST['id'];
        $sql = mysqli_query($mysqli,"UPDATE galeria SET favorito = '$check' WHERE id = '$id'")or die(mysqli_error($mysqli));
    }

    if($sql)
        echo "success";
    else
        echo "not success";
}

this code my code it works but always does the update in the last element, ex has 3 update I select the first and last it will only change the last one wanted it to change according to the id of the same someone could help me?

    
asked by anonymous 23.10.2016 / 23:34

1 answer

3

In the part that generates form

  while($aux = mysqli_fetch_array($sql)){
    $id = $aux['id'];
    $nome = $aux['nome'];
    $img = $aux['imgp'];

    echo <<<END
    <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
      <div class="hovereffect">
        <span class="abcd"></span>
          <img id="he" class="img-responsive" src="../images/imagens/galeria/big/$img" alt="$nome">
          <div class="overlay">
             <div class="btn-group" data-toggle="buttons">
                <label class="btn btn-primary cke">
                  <input type="checkbox" name="ck[]" value="$id" id="$id"><i class="fa fa-heart"></i>
                </label>
             </div>
          </div>
      </div>
  </div>
END;
  }

In the part that inserts, something like this:

require "conexao.php";

if(isset($_POST["final"])){
  $sql = mysqli_query($mysqli,
       "UPDATE galeria SET favorito = id IN( ".implode(',',$ck)." )"
     ) or die(mysqli_error($mysqli));
}

Of course ideally you should sanitize% s of% s to avoid injection. If you want to reverse the direction of checkboxes, just change id to IN .

Note that we have eliminated the loop because NOT IN will join the IDs in a single check. implode has been removed too, since you will only receive the marked fields, so you will use WHERE to determine true and false. Attention: only do this if the form contains all the fields, otherwise you will really need IN and the WHERE field.

There may be some silly syntax error, the important thing is to understand the changes and take advantage of those that are useful.

    
24.10.2016 / 01:50