dqqlziv195281 2013-06-20 12:43
浏览 47

Checkbox和Isset用PHP更新Mysql

I can't get my update checkbox function to work. I need to be able to remove or add a value which I have chosen to call checked in to my table. The code looks like following.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="all" value="<?php echo $hemsida['All']; ?>" 
<?php if($hemsida['All'] == 'checked') echo " checked"; ?> /> Alla



<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>

and the Update PHP looks like this

    if(isset($_POST['submit'])) {
    $all = ($_POST['All'] == 1) ? "checked" : "";
    $u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`=$all WHERE ID = $_POST[id]";
    mysql_query($u) or die(mysql_error());

    echo "User has been modified";
    header("Location: ..//sokh.php");
} 

The error is Undefined variable: hemsida on all parts. And also You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 33' at line 1. But I have no problem getting the data in to the Modifier or what I should call it but unable to get it out.

ANSWER !!!

I got it to work but cant answer my own question so i write it down here , i added and removed code until everything broke down. Remove the "$all = ($_POST['All'] == 1) ? checked : ;" part and now it works. I will copy the code underneath it there is an interest

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
<input type="checkbox" name="all" value="checked"   <?php if($hemsida['All'] == 'checked') echo "checked=\"checked\""; ?>/> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>

the new php

    if(isset($_POST['submit'])) {
    $u = "UPDATE hemsida SET `Comment`='$_POST[inputComment]', `Namn`='$_POST[inputName]', `All`='$_POST[all]' WHERE ID = $_POST[id]";
    mysql_query($u) or die(mysql_error());

    echo "User has been modified";
    header("Location: ..//sokh.php");
} 
  • 写回答

1条回答 默认 最新

  • doujiao9426 2013-06-20 12:47
    关注
    1. It seems that $hemsida is not available at that point in your code, but you are using it.
    2. An input type="checkbox" does notshow up in $_POST when it is not checked. , use isset.

    sidenote: you're using XHTML? the correct checked type = checked="checked"

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?