使用checkbox php mysql更新数据库 [英] Update database with checkbox php mysql

查看:147
本文介绍了使用checkbox php mysql更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 如果选中,则使用1更新数据库。

    / li>

  • 否则,如果未选中,则将其更新为0.



如果未选中,则无效。

 <?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 =postaction =< ;? echo $ _SERVER ['REQUEST_URI'];?> >

< input type =checkboxname =videoid =videovalue =<?echo $ video;?> <?php
if($ video =='1'){
echochecked ='checked';
}
else {}
echo/>
?>
< input type =submitname =submitvalue =Submit>
< / form>

<?php
}
if(isset($ _ POST ['submit']))
{
if(is_numeric '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 =refreshcontent =0/>';

}
}
// echo $ query;
// header(Location:updatesql.php);

?>我也可以使用jquery更新它吗?


解决方案

我觉得这个部分有点奇怪:

  if(is_numeric $ _POST ['video'])&& $ _POST ['video']< 2)
{
$ video1 = isset($ _ POST ['video'])? '1':'0';

您首先检查它是否为数字,然后检查是否已设置。如果没有设置,那么如果将自动变为false。换句话说, $ video 总是1.假设 $ video 只能是true / false作为已检查/未选中,不真正确定),使用它像这样:

  $ video1 = '1':'0');希望我成功地发现了这个问题:)


p> UPDATE

 < input type =checkboxname =videoid = videovalue =video<?php 
if($ video =='1'){
echochecked ='checked';
}
echo/>;

...

if(isset($ _ POST ['submit']))
{
echo $ _POST ['video']; //再次,请告诉它在这里输出!
$ video1 =(($ _POST ['video'] ==video)?'1':'0');


I want to update the database with a checkbox in checked state.

  • If it is checked then update the database with 1.
  • Else if it is unchecked then update it with 0.

It works fine but it doesn't work if unchecked.

<?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"); 

?>

Can I also use jquery to update it smoothly?

解决方案

I find this part a bit weird:

if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
    $video1 = isset($_POST['video']) ? '1' : '0';

You first check if it is numeric and then check if it is set. If it wasn't set, that if would become false automatically. In other words, $video is always 1. Assuming $video can be only true/false (or maybe it comes as "checked"/"unchecked", not really sure), use it like this:

$video1 = ($video ? '1' : '0');

Hopefully I spotted the issue successfully :)

UPDATE

<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');

这篇关于使用checkbox php mysql更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆