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?
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 theid
in the value of thecheckbox
.– Leonardo Costa
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 stringsim
andnão
instead of true and false, just add aIF( id IN..., 'sim', 'não' )
around the verification ofin
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.– Bacco