希望每个人都很好。)
实际上我得到了未定义变量的多个错误,但代码似乎完全没问题。我在我的脚本中一次又一次地遇到这个问题,我真的厌倦了它。它推迟了我的工作。
请检查代码并发送任何解决方案。 任何形式的帮助将不胜感激。提前致谢
以下是 manage-learning-material.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['subjectsid'];
}
mysql_close($con);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Manage Learning Material</title>
</head>
<body>
<h2 class="alt">COURSE VIEW </h2>
<?php
if (isset($_GET["status"]))
{
if($_GET["status"]==1)
{
?>
<div class="success">
<?php
echo("<strong>Material has been added in Course Successfully!</strong>");
?>
</div>
<?php
}
if($_GET["status"]==2)
{
?>
<div class="success">
<?php
echo("<strong>Learning Material has been Edited Successfully!</strong>");
?>
</div>
<?php
}
}
?>
<form id="form" method="post" action="manage-learning-material-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='subjectsid'>\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);
?>
<br /><br />
<label>Description:</label><br /><textarea name="description" id="description"><?php echo $des; ? ></textarea><br /> <br />
</form>
</div>
</div>
<?php include("../includes/footer.php"); ?>
</div>
</body>
</html>
<?php
}
else
{
header("Location: ".$fullpath."login/unauthorized.php");
}
?>
管理学习材料-action.php的
<?php include("../includes/config.php");?>
<?php
$id=$_POST["id"];
$title=$_POST["title"];
$des=$_POST["description"];
$subjectid=$_POST["subjectsid"];
$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."', subjectsid='".$subjectid."' WHERE (id='".$id."')");
$result=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\manage-learning-material.php on line 11
<br /><b>Notice</b>: Undefined variable: title in <b>C:\xampp\htdocs\project\teacher\manage-learning-material.php</b> on line <b>86</b><br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage- learning-material.php</b> on line <b>102</b><br />
>Literature7</option>
<option value='3'<br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>102</b><br />
>Management</option>
<option value='7'<br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>102</b><br />
>Marketing</option>
<option value='5'<br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>102</b><br />
>Science</option>
<option value='6'<br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>102</b><br />
>Science2</option>
<option value='4'<br />
<b>Notice</b>: Undefined variable: subjectid in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>102</b><br />
<b>Notice</b>: Undefined variable: des in <b>C:\xampp\htdocs\project\teacher\manage-learning- material.php</b> on line <b>110</b><br />
答案 0 :(得分:5)
你没有得到错误,你得到NOTICES ....在这种情况下,当$ title尚未定义时,你正在回显$ title的值... PHP仍然可以运行,但它正在让你知道这是你应该解决的问题......
而不是
echo $title;
使用
echo (isset($title)) ? $title : '';
或者在尝试从数据库中读取之前为这些变量定义默认值'
并学习如何执行不会让您对SQL注入攻击开放的数据库查询
答案 1 :(得分:3)
我不知道从哪里开始编写代码......
您的变量在此处定义
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$title = $row['title'];
$des = $row['description'];
$subjectid = $row['subjectsid'];
}
您需要在循环
之外首先定义它们$id = "";
$title = "";
$des = "";
$subjectid = "";
while ( $row = mysql_fetch_array($result) ) {
// .. bala bla bla
}
其次以下行是错误的
<textarea name="description" id="description"><?php echo $des; ? ></textarea><br /> <br />
应该是
<textarea name="description" id="description"><?php echo $des; ?></textarea><br /> <br />
最后
来自mysql_***
上的PHP DOC
不鼓励使用此扩展程序。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。该功能的替代方案包括:
答案 2 :(得分:3)
或者这个:
if (isset($row['title'])&&!empty($row['title'])){$title=$row['title'];}else{$title='';}
<textarea name="description" id="description"><?php echo $title; ?></textarea>
或
<textarea name="description" id="description"><?php echo (isset($row['title'])&&!empty($row['title'])) ? $row['title'] : ''; ?></textarea>
或
<textarea name="description" id="description"><?php echo (isset($title)&&!empty($title)) ? $title : ''; ?></textarea>
享受=)
答案 3 :(得分:3)
字段将为空。 看下一个代码:
$result = mysql_query("SELECT * FROM `courses` WHERE `id` = '".$courseid."'");
if(!$result||mysql_num_rows($result)<1){echo 'empty result';}
else{
while($row = mysql_fetch_array($result))
{
echo (isset($row['id'])&&!empty($row['id'])) ? $row['id'] : '';
echo'<br>';
echo (isset($row['title'])&&!empty($row['title'])) ? $row['title'] : '';
echo'<br>';
echo (isset($row['description'])&&!empty($row['description'])) ? $row['description'] : '';
echo'<br>';
echo (isset($row['subjectsid'])&&!empty($row['subjectsid'])) ? $row['subjectsid'] : '';
}
}
测试此代码并享受=)