Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
我知道它的基本原理,但我已经编码了8个小时,这让我卡住了。在我的更新查询的第14行接收我所有文章的未定义索引。我希望能在这件事上得到帮助,谢谢。

<html>
<head>
</head>
<body>
<h1><center><i>Edit/Add Course ID</i></center></h1>
<?php
$con = mysql_connect("localhost","u/n","p/w");
if (!$con){
die ("Can not connect: " . mysql_error());
}
mysql_select_db("my_db",$con);

if(isset($_POST['submit'])){
$UpdateQuery = "UPDATE course SET cours_num='$_POST[coursnum]',
cours_title='$_POST[courstitle]', cours_desc='$_POST[coursdesc]' WHERE
cours_num='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);

};
$sql = "Select * FROM course";
$myData = mysql_query($sql,$con);

while($record = mysql_fetch_array($myData)){
echo "<form action=mydata.php method=post>";
echo "<b>Course Number:</b></br>";
echo "<input type=text maxlength=10 size=10 readonly name=coursnum value=" .
$record['cours_num'].">";
echo "</br></br>";
echo "<b>Course Title:</b><br/>";
echo "<input type=text maxlength=10 size=10 name=courstitle value=" .
$record['cours_title'].">";
echo "</br></br>";
echo "<b>Course Description:</b></br>";
echo "<input type=text name=coursdesc max length=50 size=50 value=" .
$record['cours_desc'].">";
echo "</br></br>";
echo "<input type=hidden name=hidden value=" . $record['cours_num'].">";
echo "<center><input type=submit name=submit value=Edit></center>";
echo "</form>";
}

mysql_close($con);

?>
</body>
</html>

最佳答案

由于以下行,您将得到错误

$UpdateQuery = "UPDATE course SET cours_num='$_POST[coursnum]',
cours_title='$_POST[courstitle]', cours_desc='$_POST[coursdesc]' WHERE
cours_num='$_POST[hidden]'";

你可以用
$UpdateQuery = sprintf("UPDATE course SET cours_num='%d' ,
                cours_title='%s',
                cours_desc='%s',
                WHERE  cours_num = '%d'", mysql_real_escape_string($_POST['coursnum']), mysql_real_escape_string($_POST['courstitle']), mysql_real_escape_string($_POST['coursdesc']), mysql_real_escape_string($_POST['hidden']));

你还应该确保你的变量总是被设置的
例子
$_POST['coursnum'] = isset($_POST['coursnum']) ? $_POST['coursnum'] : null ;

09-26 07:32