通过PHP更新MySQL数据时出错 [英] Error updating MySQL data through PHP
问题描述
我尝试使用复选框更新数据.但是,如果未选中一个或多个复选框,则会返回一条通知:
I try to update the data making use of checkboxes. But when one or more of the checkboxes is not checked, a notice is returned:
注意:未定义的索引:第12行E:\ wamp \ www \ HOSPITAL \ update.php中的stats2
Notice: Undefined index: stats2 in E:\wamp\www\HOSPITAL\update.php on line 12
注意:未定义的索引:第12行E:\ wamp \ www \ HOSPITAL \ update.php中的stats3
Notice: Undefined index: stats3 in E:\wamp\www\HOSPITAL\update.php on line 12
注意:未定义的索引:第12行E:\ wamp \ www \ HOSPITAL \ update.php中的stats5
Notice: Undefined index: stats5 in E:\wamp\www\HOSPITAL\update.php on line 12
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Hospital", $con);
mysql_query("UPDATE t2 SET HOSPNUM ='$_POST[hnum]', ROOMNUM='$_POST[rnum]', ADDATE= '$_POST[ad8]', ADTIME='$_POST[adtym]', LASTNAME='$_POST[lname]', FIRSTNAME='$_POST[fname]', MIDNAME='$_POST[mname]', CSTAT='$_POST[cs]', AGE='$_POST[age]', BDAY='$_POST[bday]', ADDRESS='$_POST[ad]', SEX='$_POST[sex]',
STAT='$_POST[stats1]', STAT2='$_POST[stats2]', STAT3='$_POST[stats3]', STAT4='$_POST[stats4]', STAT5='$_POST[stats5]', STAT6='$_POST[stats6]', STAT7='$_POST[stats8]', STAT8='$_POST[stats8]', NURSE='$_POST[nurse]'
WHERE TELNUM ='$_POST[telnum]'");
mysql_close($con)
?>
您能帮我,使通知不显示吗?
Could you help me, so that the notice would not show up?
推荐答案
这是复选框的已定义行为-仅在设置复选框后才将其包含在表单数据中.
This is the defined behavoir for checkboxes - only when set are they included in the form data.
您应该使用isset()来确定该复选框是否被选中.
You should use isset() to determine if the checkbox is ticked.
将其更改为
STAT='".isset($_POST['stats1']).",
STAT2='".isset($_POST['stats2']).",
STAT3='".isset($_POST['stats3']).",
STAT4='".isset($_POST['stats4']).",
STAT5='".isset($_POST['stats5']).",
STAT6='".isset($_POST['stats6']).",
STAT7='".isset($_POST['stats8']).",
STAT8='".isset($_POST['stats8'])."
另一种可行的解决方法是在复选框前添加一个具有相同名称的隐藏变量:例如
Another workaround which works is to add a hidden variable, with the same name, before the checkbox: e.g.
<form action='t1.php' method='post'>
<input type='hidden' name="cb1" value="0">
<input type='checkbox' name="cb1" title='test'>
<input type='submit' >
</form>
<?php
print_r($_POST);
?>
这篇关于通过PHP更新MySQL数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!