复选框选中或未选中 boolean php mysql

发布于 2025-01-01 08:23:41 字数 2346 浏览 0 评论 0原文

我正在尝试使用复选框制作此表单,获取值 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-01-08 08:23:41

如果未选中复选框,您将不会在 $_POST 变量中得到任何键。如果选中,则将设置键并且值将是复选框的值。

假设名称“user[]”有效,类似于:

<input type="checkbox" name="user[]" value="aa" />
<input type="checkbox" name="user[]" value="bb" checked="checked" />
<input type="checkbox" name="user[]" value="cc" checked="checked" />
<input type="checkbox" name="user[]" value="dd" />
<input type="checkbox" name="user[]" value="ee" checked="checked" />

您会期望 $_POST 为:

array(
    "user" => array(
        "1" => "bb"
        "2" => "cc"
        "4" => "ee"
    )
);

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:

<input type="checkbox" name="user[]" value="aa" />
<input type="checkbox" name="user[]" value="bb" checked="checked" />
<input type="checkbox" name="user[]" value="cc" checked="checked" />
<input type="checkbox" name="user[]" value="dd" />
<input type="checkbox" name="user[]" value="ee" checked="checked" />

You would expect $_POST to be:

array(
    "user" => array(
        "1" => "bb"
        "2" => "cc"
        "4" => "ee"
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文