复选框选中或未选中 boolean php mysql
我正在尝试使用复选框制作此表单,获取值 1 或 0(类似于布尔值),然后在 mysql 上更新它,但我还没有找到方法。也许你能帮忙。我将发布表格和接收它的代码。我确信可能存在循环冲突,但我尝试了所有方法但无法使其正常工作。提前谢谢。
表单
<form action="esconde_cat.php" method="post">
<table width="300px" align="center" width="140" border="1">
<tr>
<td></td>
<td width="200">Categoria</td>
<td width="100">Mostrar</td>
</tr>
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT * FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());
while ($linha=mysql_fetch_array($res)) {
?>
<tr>
<td><input name="user[]" type="checkbox" value="true"/></td>
<td><?=$linha['1']; ?></td>
<td><?php if ($linha['3'] == TRUE){ echo 'SIM'; } else {echo 'NÃO'; } ?></td>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<table align="center">
<tr>
<td>
<input type="submit" name="Delete" type="button" value="Alterar">
</td>
</tr>
</table>
</form>
接收表单并更新 MYSQL 的代码
<?php
include "conecta.php";
include "verifica.php";
\\THIS PART WORKS, GETS IF THE CHECKBOX IS CHECKED AND RECORD IT ON DB
if(isset($_POST['user'])) {
foreach ($_POST['user'] as $key => $read){
$read =(@$_POST['user']=='0')? $_POST['user']:'1';
echo $read;
$sql = "UPDATE categorias SET mostrar='$read' WHERE ordem='$key'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
\\ON THIS PART OF THE CODE THAT I CANT GET THE VARIABLE TO GET THE
\\VALUE FROM CHEBOXES THAT ARE UNCHECKED RECORDED TO DATABASE
if (!isset($_POST['user'])) {
foreach ($_POST['user'] as $chave => $leia) {
$leia =(@$_POST['user']=='1')? $_POST['user']:'0';
echo $leia;
$sql = "UPDATE categorias SET mostrar='$leia' WHERE ordem='$chave'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
?>
I am trying to make this form with checkboxes get values 1 or 0, sort of a boolean, and UPDATE it on mysql, but I havent found a way yet. Maybe you can help. I'll post the form and the code that receives it. I am sure there might be a conflict of loops, but I tryed everything and couldnt get it working. Tks in advance.
THE FORM
<form action="esconde_cat.php" method="post">
<table width="300px" align="center" width="140" border="1">
<tr>
<td></td>
<td width="200">Categoria</td>
<td width="100">Mostrar</td>
</tr>
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT * FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());
while ($linha=mysql_fetch_array($res)) {
?>
<tr>
<td><input name="user[]" type="checkbox" value="true"/></td>
<td><?=$linha['1']; ?></td>
<td><?php if ($linha['3'] == TRUE){ echo 'SIM'; } else {echo 'NÃO'; } ?></td>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<table align="center">
<tr>
<td>
<input type="submit" name="Delete" type="button" value="Alterar">
</td>
</tr>
</table>
</form>
THE CODE THAT RECEIVES THE FORM AND UPDATE MYSQL
<?php
include "conecta.php";
include "verifica.php";
\\THIS PART WORKS, GETS IF THE CHECKBOX IS CHECKED AND RECORD IT ON DB
if(isset($_POST['user'])) {
foreach ($_POST['user'] as $key => $read){
$read =(@$_POST['user']=='0')? $_POST['user']:'1';
echo $read;
$sql = "UPDATE categorias SET mostrar='$read' WHERE ordem='$key'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
\\ON THIS PART OF THE CODE THAT I CANT GET THE VARIABLE TO GET THE
\\VALUE FROM CHEBOXES THAT ARE UNCHECKED RECORDED TO DATABASE
if (!isset($_POST['user'])) {
foreach ($_POST['user'] as $chave => $leia) {
$leia =(@$_POST['user']=='1')? $_POST['user']:'0';
echo $leia;
$sql = "UPDATE categorias SET mostrar='$leia' WHERE ordem='$chave'";
$res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果未选中复选框,您将不会在
$_POST
变量中得到任何键。如果选中,则将设置键并且值将是复选框的值。假设名称“user[]”有效,类似于:
您会期望
$_POST
为:If a checkbox is left unchecked you will get NO key in the
$_POST
variable. If it's checked the key will be set and the value will be the value of the checkbox.Assuming a name of "user[]" works, with something like:
You would expect
$_POST
to be: