how to update the database using checkbox

Asked

Viewed 723 times

0

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’t catch the id of the respective how can I do this? below is the part of the 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 my code it even works but always update the last element, ex i have 3 update select the first and the last it will only change the last wanted it to change as the id of the same someone could help me?

1 answer

3


In the part which generates the 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 you insert, something like that:

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 the ids to avoid Injection. If you want to reverse the direction of checkboxes, simply change the IN for NOT IN.

Note that we eliminate the loop, because the implode will merge Ids into one check only. O WHERE was removed as well, as you will only receive the marked fields, so will use the IN to determine true and false. Note: only do this if the form contains all fields, otherwise you will really need the WHERE and more the countryside HIDDEN.

You may have some silly syntax error, the important thing is to understand the changes and take advantage of the ones that are useful.

  • intao I can not pass id in value no checkbox because my values will be entered in the favorite field of the bank that will receive a string if and yes or no so I cannot pass the id in the value of the checkbox.

  • That’s what the above code does. It stores in the favorite field whether checkboxes are checked or not, and it’s just what the id field. If you want string sim and não instead of true and false, just add a IF( id IN..., 'sim', 'não' ) around the verification of in above. In fact, it would be better to adjust the column in DB for true and false than to change the code, it does not have much advantage to store string boolean value in Mysql. Be that as it may, putting "yes" and "no" in the checkbox value is definitely not the way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.