0

希望那里的每个人都很好:)

实际上我得到了未定义变量的多个错误,但代码似乎完全没问题。我在我的脚本中一次又一次地遇到这个问题,我真的受够了。它耽误了我的工作。

请检查代码并发送任何解决方案。任何形式的帮助将不胜感激。提前致谢

这是我的 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 />
4

4 回答 4

5

你没有得到错误,你得到通知......在这种情况下,当 $title 尚未定义时,你正在回显 $title 的值...... PHP 仍然可以运行,但它让你知道这是你应该解决的问题......

代替

echo $title;

利用

echo (isset($title)) ? $title : '';

或在尝试从数据库中读取之前为您被警告的这些变量定义默认值

并了解如何执行不会让您受到 SQL 注入攻击的数据库查询

于 2012-10-03T10:37:52.363 回答
4

如果数据库中的值为空,则字段将为空。看下一个代码:

$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'] : '';
}
}

测试这段代码并享受=)

于 2012-10-04T00:38:22.557 回答
3

不知道从哪里开始你的代码......

您的变量在此处定义

while($row = mysql_fetch_array($result))
{
   $id=$row['id'];
   $title = $row['title'];
   $des = $row['description'];
   $subjectid = $row['subjectsid'];
}

您需要先在 while 循环之外定义它们

$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 />

最后

从 PHP 文档开始mysql_***

不鼓励使用此扩展程序。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。另请参阅 MySQL:选择 API 指南和相关的常见问题解答以获取更多信息。此功能的替代方案包括:

于 2012-10-03T10:38:29.523 回答
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>

享受=)

于 2012-10-03T10:51:23.200 回答