基本的东西,但我不明白为什么它不起作用 我想在数据库中保存一个复选框的状态,输入布尔值,如果选中则为1否则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'];
答案 0 :(得分:2)
这将有效:
if(isset($_POST['stok'])){
//$stok is checked and value = 1
$stok = $_POST['stok'];
}
else{
//$stok is nog checked and value=0
$stok=0;
}
答案 1 :(得分:0)
尝试在if。
中将$_POST['stok'] === 'on'
更改为$_POST['stok'] === '1'
答案 2 :(得分: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."'");
必要时添加数据卫生。
答案 3 :(得分: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;
}
?>
答案 4 :(得分: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>
答案 5 :(得分:0)
$stock = $_POST['stok'] ?? 0; // Null coalescing Since PHP7