我想使用处于选中状态的复选框更新数据库。
它工作正常但如果未选中则无效。
<?php
include('lib/db.php');
$facebook_id ="10001088";
$query1 = "SELECT `video`,`quran`,`medical`,`groups` FROM `man_facebook`.`users` WHERE `facebook_id`='$facebook_id'";
$result1 = mysql_query($query1);
while($result = mysql_fetch_array($result1))
{
$video = $result['video'];
$quran = $result['quran'];
$medical = $result['medical'];
$groups = $result['groups'];
echo $video;
// echo $quran;
?>
<form method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>" >
<input type="checkbox" name="video" id="video" value="<?echo $video;?>" <?php
if($video == '1'){
echo "checked='checked'";
}
else {}
echo "/>"
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
if (isset($_POST['submit']))
{
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
echo $video1;
$query = mysql_query("UPDATE `man_facebook`.`users`
SET `video` ='$video1'
WHERE `facebook_id`='$facebook_id'");
$video = $video1;
echo '<meta http-equiv="refresh" content="0" />';
}
}
//echo $query;
//header("Location: updatesql.php");
?>
我还可以使用jquery顺利更新它吗?
答案 0 :(得分:0)
我发现这部分有点奇怪:
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
首先检查它是否为数字,然后检查它是否已设置。如果未设置,则if将自动变为false。换句话说,$video
始终是1.假设$video
只能是真/假(或者它可能是“已检查”/“未选中”,不是很确定),请按照以下方式使用它: / p>
$video1 = ($video ? '1' : '0');
希望我成功地发现了这个问题:)
<强>更新强>
<input type="checkbox" name="video" id="video" value="video" <?php
if($video == '1'){
echo "checked='checked'";
}
echo "/>";
...
if (isset($_POST['submit']))
{
echo $_POST['video']; // again, please tell what it outputs here!!!
$video1 = (($_POST['video'] == "video") ? '1' : '0');
答案 1 :(得分:-1)
复选框更新
if (isset($_POST["send"]) && $_POST["send"] == "ok") {
foreach ($_POST['membre'] as $id => $data) {
mysql_query("UPDATE tablename SET param1='" . $data['param'] . "' WHERE id='" . $id . "'");
}
}
<form>
<input type="hidden" name="send" value="ok" />
<td class="cel2" style=" width: 100px;">
<input name="membre[<?php print $val['id']; ?>][param]" type="checkbox" style="margin-top: 7px; width: 30px;" value="1" id="param" <?php if ($val['p'] == '1') print 'checked'; ?> />
</td>
</form>