4

一些基本的东西,但我不明白为什么它不起作用我想将复选框的状态保存在数据库中,输入布尔值,如果选中 1 else 0 这是我的代码:

<input type="checkbox" name="stok"  value="1"
  <?php if(isset($_POST['stok'])) echo "checked='checked'"; ?>
/>
<?php
    if (isset($_POST['stok']) && !empty($_POST['stok']) && $_POST['stok'] === 'on')               
        $stok = 1;  
    else 
        $stok = 0;      

    mysql_query ("update tab set etat_vendu=$stok where id=$big");
?>  

//this is my var $stok=$row['stok'];   
4

6 回答 6

2

这将起作用:

if(isset($_POST['stok'])){
    //$stok is checked and value = 1
    $stok = $_POST['stok'];
}
else{
    //$stok is nog checked and value=0
    $stok=0;
}
于 2013-05-23T12:49:38.210 回答
0

尝试更改$_POST['stok'] === 'on'$_POST['stok'] === '1'您的 if。

于 2013-05-23T12:18:14.690 回答
0

您的 if 语句可以更短,更短,因为您已经在传递一个值:)

if (isset($_POST['stok']))
    $stok = $_POST['stok']; // equals 1 since that's the given value
else
    $stok = 0;

编辑:不要忘记检查您的UPDATE()陈述:

mysql_query("UPDATE tab SET etat_vendu='".$stok."' WHERE id='".$big."'");

在必要时添加数据清理。

于 2013-05-23T12:19:14.160 回答
0

我想我知道你现在想要什么

$result = mysqli_query($con,"SELECT etat_vendu FROM tab WHERE id=".$big);
    while($row = mysqli_fetch_array($result))
    {
        $stok = $row['etat_vendu'];
    }

还有你的复选框

    <form action='index.php' method='post'>
<?php
    if($stok != 0){
    echo "<input type='checkbox' name='stok'  value='1' checked='checked'/>";
    }
    else{
    echo "<input type='checkbox' name='stok'  value='1'/>";
    }
?>
    <input type='submit' name='submit' value='submit'>
    </form>
<?php
            if(isset($_POST['stok'])){
                //$stok is checkend and value = 1
                $stok = $_POST['stok'];
            }
            else{
                $stok=0;
            }
?>
于 2013-05-23T13:24:11.387 回答
0

就如此容易

if($fetch['proved'] === '1') {
   $p1 = 1;
   $p0 = 0;
} else {
   $p1 = 0;
   $p0 = 1;
}

<select name="proved">
  <option value="<?php echo $p0 ?>"><?php echo $p0 ?></option>
  <option selected value="<?php echo $p1 ?>"><?php echo $p1 ?></option>
</select>
于 2015-12-26T23:00:47.063 回答
0
$stock = $_POST['stok'] ?? 0; // Null coalescing Since PHP7
于 2021-03-31T07:46:59.633 回答