可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
大家好,我在更新记录时遇到问题..当我点击编辑按钮时,它不会更新记录,但会对所有帖子变量给出以下错误..我真的很感激任何评论或帮助......
这是courses-edit.php
<?php include("../includes/config.php"); ?>
<?php
if ($_SESSION["isteacher"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$courseid=$_GET["id"];
$result = mysql_query("SELECT * FROM courses WHERE (id='".$courseid."')");
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$title = $row['title'];
$des = $row['description'];
$subjectid = $row['subjects-id'];
}
mysql_close($con);
?>
<script type="text/javascript">
$(function() {
$("form").validity(function() {
$("#title")
.require("This Field Must Be Filled!!")
});
});
</script>
<?php include("../includes/header.php"); ?>
<?php include("includes/nav.php"); ?>
<div id="maincontent">
<div class="span-24 last">
<div id="breadcrumbs">
<a href="index.php">Home</a> >
<a href="manage-courses.php">Manage Courses</a> >
<a href="courses-list.php">List Courses</a> >
Edit Course
</div>
</div>
<?php include("../teacher/includes/manage-courses-aside.php"); ?>
<div class="span-18 last">
<h2 class="alt">Edit Course</h2>
<form id="form" method="post" action="courses-edit-action.php">
<input type="hidden" value="<?php echo $courseid; ?>" name="id" />
<label>Course Name:</label><input type="text" name="title" id="title" class="text" value="<?php echo $title; ?>" /><br /><br />
<label>Choose Subject:</label>
<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT * FROM subjects");
echo "<select name='subjects-id'>\n";
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id'] . "'";
if ($subjectid==$row['id'])
echo 'selected="selected"';
echo " >" . $row['subjectname'] . "</option>\n";
}
echo "</select>\n";
mysql_close($con);
?>
<label>Description:</label><textarea name="description" id="description"><?php echo $des; ?></textarea><br /> <br />
<input type="submit" value="Edit Course" class="button" />
</form>
<?php include("../includes/footer.php"); ?>
<?php
}
else
{
header("Location: ".$fullpath."login/unauthorized.php");
}
?>
这是courses-edit-action.php
<?php include("../includes/config.php");?>
<?php
$id=$_POST["id"];
$title=$_POST["title"];
$des=$_POST["description"];
$subjectid=$_POST["subjects-id"];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("ombts", $con);
$query=("UPDATE courses SET title='".$title."', description='".$des."', 'subjects- id'='".$subjectid."' WHERE (id='".$id."')");
$query=mysql_query($query);
if($result){
echo header("Location:manage-courses.php?status=2");
}
mysql_close($con);
?>
并且警告是
Notice: Undefined index: id in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 4
Notice: Undefined index: title in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 5
Notice: Undefined index: description in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 6
Notice: Undefined index: subjects-id in C:\xampp\htdocs\project\teacher\courses-edit-action.php on line 7
答案 0 :(得分:2)
像这样修改你的脚本......
$id= isset($_POST["id"]) ?$_POST["id"] : 0 ;
$title= isset($_POST["title"]) ? $_POST["title"] : '' ;
$des=isset($_POST["description"]) ? $_POST["description"] : '';
$subjectid= isset($_POST["subjects-id"]) ? $_POST["subjects-id"] : '';
编辑 另外,我在这里看到一个错误......
$query=mysql_query($query);
if($result){
应该是
$result=mysql_query($query);
if($result){
如果仍然无法更新表,请在执行mysql_query之前打印$ query变量。告诉我你的看法,我会帮助你......
答案 1 :(得分:2)
您尝试访问
$id=$_POST["id"];
$title=$_POST["title"];
$des=$_POST["description"];
$subjectid=$_POST["subjects-id"];
这个POST变量。但是当你第一次访问脚本时,这个值没有在POST变量中设置。
使用isset清除值。
if(!isset($_POST["id"]) {
$_POST["id"] = '';
}
或安全检查新变量中的值
$id = isset($_POST["id"]) ? $_POST["id"] : 0;
例如。
答案 2 :(得分:2)
@Stony的答案是检查您是否发送任何POST数据的好方法 - 如果没有,请不要尝试获取不存在的数据。
现在,如果您认为$_POST["id"];
应该存在,那么就会有一些缺失。您使用GET而不是POST吗?
要查看提交的数据,请执行print_r($_POST)
。如果这是空白,请执行print_r($_REQUEST)
。如果这仍然是空白,则不会从上一页提交数据。
您还可以在网址&id=75
中添加此内容进行检查(如果网址中没有其他参数,则可以?id=75
)。这将提交数据,您将看到少一个错误。