gpt4 book ai didi

php - 通知 :undefined variable

转载 作者:行者123 更新时间:2023-11-30 23:55:23 24 4
gpt4 key购买 nike

希望那边的人都好:)

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

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

这是我的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");

}
?>

manage-learning-material-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 />

最佳答案

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

代替

echo $title;

使用

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

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

并学习如何执行不会让您受到 SQL 注入(inject)攻击的数据库查询

关于php - 通知 :undefined variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com