用户选择一个名称,它会显示一个用于提交某些数据的表单。我希望根据所选名称将这些数据存储到右表中。当我单击“保存”时,出现错误:
注意:未定义的索引:第20行的C:\ XAMPP \ htdocs \ statistics \ savelecscores.php中的讲师
<?php
include 'connect.php';
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
<script type="text/javascript">
function resetForm(){
// clearing selects
var selects = document.getElementsByTagName('select');
for (var i = 0; i<selects.length; i++)
selects[i].selectedIndex = 0;
return false;
}
window.load(resetForm());
</script>
</head>
<body>
<div id="nav">
<div id="nav_wrapper">
<ul>
<li><a href="#">Principal</a>
<ul>
<li><a href="../../statistics/principalLecturerForm.php">Lecturer Scores</a></li>
<li><a href="../../statistics/principalServicesForm.php">Services Scores</a></li>
<li><a href="../../statistics/principalLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Academic Director</a>
<ul>
<li><a href="../../statistics/ac_directorLecturerForm.php">Lecturer Scores</a></li>
<li><a href="../../statistics/ac_directorServicesForm.php">Services Scores</a></li>
<li><a href="../../statistics/ac_directorLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Lecturer</a>
<ul>
<li><a href="../../statistics/lecturerForm.php">Lecturer</a></li>
<li><a href="../../statistics/lecLecturerYears.php">Lecturer Over the Years</a></li>
</ul>
</li>
<li><a href="#">Admin</a>
<ul>
<li><a href="../../statistics/adminFormLecturer.php">Save Lecturer Scores</a></li>
<li><a href="../../statistics/adminFormServices.php">Save Services Scores</a></li>
<li><a href="../../statistics/upload.php">Update Lecturers</a></li>
</ul>
</li>
<li><a href="../../statistics/logout.php">Logout</a></li>
</ul>
</div>
</div>
<br />
<br />
<br />
<div id="lecform">
<form action="savelecscores.php" method="POST">
A1: <input type="text" name="a1" size="5"> <br /><br/>
A2: <input type="text" name="a2" size="5"> <br /><br/>
A3: <input type="text" name="a3" size="5"> <br /><br/>
L1: <input type="text" name="l1" size="5"> <br /><br/>
L2: <input type="text" name="l2" size="5"> <br /><br/>
L3: <input type="text" name="l3" size="5"> <br /><br/>
L4: <input type="text" name="l4" size="5"> <br /><br/>
L5: <input type="text" name="l5" size="5"> <br /><br/>
L6: <input type="text" name="l6" size="5"> <br /><br/>
L7: <input type="text" name="l7" size="5"> <br /><br/>
LEC AVG: <input type="text" name="lavg" size="5"> <br /><br/>
R1: <input type="text" name="r1" size="5"> <br /><br/>
R2: <input type="text" name="r2" size="5"> <br /><br/>
U1: <input type="text" name="u1" size="5"> <br /><br/>
U2: <input type="text" name="u2" size="5"> <br /><br/>
U3: <input type="text" name="u3" size="5"> <br /><br/>
<b>Year:<b/>
<select name="year">
<option value="Choose">Please select..</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option></select><br/><br/>
<b>Unit Name:<b/>
<select id="SelectUnitName" name="unit_name">
<?php
$sql = mysql_query("SELECT unit_name FROM unit");
while ($row = mysql_fetch_array($sql)){
echo "<option value='".$row['unit_name']."'>".$row['unit_name']."</option>";
}
?>
</select>
</form><br /><br/>
<br/>
<input type="submit" name="Submit"value="Save" >
<input type="reset" name="reset" value="Clear">
</form>
</div>
<br />
<br />
<div id="footer">
Copyright e-Statistics 2015
</div>
</body>
</html>
保存讲师分数
<?php
define('DB_NAME','mydb');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');
$connect = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$connect){
die('Could not connect:'.mysql_error());
}
$db_selected=mysql_select_db(DB_NAME,$connect);
if(!$db_selected){
die('Can\'t use'.DB_NAME.':'.mysql_error());
}
$lecturer = $_POST['lecturer'];
$lecturers = array(
'dimopoulos',
'lagkas',
'kehagias',
'chrysochoou'
);
$value1=$_POST['a1'];
$value2=$_POST['a2'];
$value3=$_POST['a3'];
$value4=$_POST['l1'];
$value5=$_POST['l2'];
$value6=$_POST['l3'];
$value7=$_POST['l4'];
$value8=$_POST['l5'];
$value9=$_POST['l6'];
$value10=$_POST['l7'];
$value11=$_POST['lavg'];
$value12=$_POST['r1'];
$value13=$_POST['r2'];
$value14=$_POST['u1'];
$value15=$_POST['u2'];
$value16=$_POST['u3'];
$value17=$_POST['year'];
$value18=$_POST['unit_name'];
if (in_array($lecturer, $lecturers) && in_array($year, $years)) {
$sql = "INSERT INTO $lecturer (a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3,year,unit_name) VALUES('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13','$value14','$value15','$value16','$value17','$value18')";
$result = mysql_query($sql);
}
else {
echo "No data have been inserted";
}
?>
答案 0 :(得分:1)
您没有输入,也没有在表单中选择“讲师”,因此未设置$_POST['lecturer']
。
在你的表格中你应该有:
<select id="SelectLecturer" name="lecturer">
<?php
$lecturers = array(
'dimopoulos',
'lagkas',
'kehagias',
'chrysochoou'
);
foreach ($lecturers as $lecturer){
echo "<option value='$lecturer'>$lecturer</option>";
}
?>
</select>
然后在您的PHP代码中,您将$_POST['lecturer']
设置为选定的讲师作为值。
注意!您的代码很容易受到mysql注入攻击。在执行查询之前,您应该使用预准备语句(PDO或mysqli),或者至少使用mysql_real_escape_string
来清理每个$_POST
值!