我有一个php表单,但每次我打开php文件时,我都会收到这些php通知错误:
注意:未定义的索引:第37行的/web/stud/u0867587/MOBILEPHP/exam_interface.php中的sessionid
注意:未定义的索引:第38行/web/stud/u0867587/MOBILEPHP/exam_interface.php中的moduleid
注意:未定义索引:第39行/web/stud/u0867587/MOBILEPHP/exam_interface.php中的teacherid
注意:未定义的索引:第40行的/web/stud/u0867587/MOBILEPHP/exam_interface.php中的studentid
注意:未定义的索引:第41行/web/stud/u0867587/MOBILEPHP/exam_interface.php中的等级
当我点击提交按钮时,通知会消失,但我需要做什么,以便在打开php文档时,未定义索引上没有通知错误?
以下是编码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exam Interface</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="exam_interface.php" method="post" name="sessionform"> <!-- This will post the form to its own page"-->
<p>Session ID: <input type="text" name="sessionid" /></p> <!-- Enter Session Id here-->
<p>Module Number: <input type="text" name="moduleid" /></p> <!-- Enter Module Id here-->
<p>Teacher Username: <input type="text" name="teacherid" /></p> <!-- Enter Teacher here-->
<p>Student Username: <input type="text" name="studentid" /></p> <!-- Enter User Id here-->
<p>Grade: <input type="text" name="grade" /></p> <!-- Enter Grade here-->
<p>Order Results By: <select name="order">
<option name="noorder">Don't Order Results</option>
<option name="ordersessionid">Session ID</option>
<option name="ordermoduleid">Module Number</option>
<option name="orderteacherid">Teacher Username</option>
<option name="orderstudentid">Student Username</option>
<option name="ordergrade">Grade</option>
</select>
<p><input type="submit" value="Submit" /></p>
</form>
<?php
$username="u0867587";
$password="xxxxxxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$sessionid = $_POST['sessionid'];
$moduleid = $_POST['moduleid'];
$teacherid = $_POST['teacherid'];
$studentid = $_POST['studentid'];
$grade = $_POST['grade'];
$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade')");
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['StudentId'] . "</td>";
echo "<td>" . $row['Forename'] . "</td>";
echo "<td>" . $row['SessionId'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "<td>" . $row['Mark'] . "</td>";
echo "<td>" . $row['ModuleName'] . "</td>";
echo "<td>" . $row['TeacherId'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
</body>
</html>
答案 0 :(得分:2)
这是因为表单尚未发布,并且这些值为空。替换为:
$sessionid = isset($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset($_POST['grade']) ? $_POST['grade'] : NULL;
这些必须在用于数据库查询之前进行清理,以保护您的应用程序免受SQL注入攻击。
$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);
强烈建议从数据库调用中删除@
,因为它会隐藏可能由这些调用产生的错误消息。相反,请使用ini_set("display_errors", 0);
以避免在生产代码中显示错误。
// Remove the @
@mysql_select_db(...)
答案 1 :(得分:1)
替换这些行:
$sessionid = $_POST['sessionid'];
$moduleid = $_POST['moduleid'];
$teacherid = $_POST['teacherid'];
$studentid = $_POST['studentid'];
$grade = $_POST['grade'];
使用:
$sessionid = isset($_POST['sessionid']) ? $_POST['sessionid'] : '';
$moduleid = isset($_POST['moduleid']) ? $_POST['moduleid'] : '';
$teacherid = isset($_POST['teacherid']) ? $_POST['teacherid'] : '';
$studentid = isset($_POST['studentid']) ? $_POST['studentid'] : '';
$grade = isset($_POST['grade']) ? $_POST['grade'] : '';
另外,我建议您从粘贴在这里的源代码中删除密码!
答案 2 :(得分:0)
第一次打开php文件时,已经为变量分配了$ _POST值。 但是,目前还没有确定。 在分配之前尝试使用if(isset($ _ POST ['name']))来防止这种情况。
答案 3 :(得分:0)
你需要提供:
$sessionid
$moduleid
$teacherid
$studentid
$grade
变量是这样的默认值:
$sessionid = 0;
$moduleid = 0;
$teacherid = 0;
$studentid = 0;
$grade = 0;
然后使用它们来获取$ _POST数组值,如下所示:
$sessionid = $_POST['sessionid'];
$moduleid = $_POST['moduleid'];
$teacherid = $_POST['teacherid'];
$studentid = $_POST['studentid'];
$grade = $_POST['grade'];
所以最终的代码是:
$sessionid = 0;
$moduleid = 0;
$teacherid = 0;
$studentid = 0;
$grade = 0;
$sessionid = $_POST['sessionid'];
$moduleid = $_POST['moduleid'];
$teacherid = $_POST['teacherid'];
$studentid = $_POST['studentid'];
$grade = $_POST['grade'];
答案 4 :(得分:0)
那是因为即使未设置变量,也要分配变量。
对isset();
变量
empty();
或$_POST[]
if isset($_POST['variable']){
$variable = $_POST['variable'];
}
不要把它打开。
答案 5 :(得分:0)
第一次打开页面时,代码会尝试查找预期发布的数据,但由于您尚未提交表单,因此不存在。你需要检查一下$ _POST ['sessionid'];等等发布然后将值赋给变量。
答案 6 :(得分:0)
我建议在开头只使用一个验证。
if (!empty($_POST)) {
// Get variables from $_POST
}