【发布时间】:2012-02-18 14:45:01
【问题描述】:
我正在开发一个系统,我想检查是否存在记录。如果存在记录,则它不会记录数据,而是返回到表单。如果数据不存在,则继续将数据记录到数据库中。
HTML 表单:
<form name="studentform" onSubmit="return validate_form ( );" action="queries/insert.php" method="post">
Student Number: <input type="text" name="studentnumber"/>
College:
<select name="college" id=a></select>
Course:
<select name="course" id=b></select>
<input type="radio" name="status" value="regular" />Regular
<input type="radio" name="status" value="irregular" />Irregular
<br><br>
<hr>
<br>
Name:
<input type="text" name="lname">
<input type="text" name="fname">
<input type="text" name="mname">
Address:
<input type="text" name="address" />
<br><br>
Gender:
<select name="gender">
<option value="">---</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" value="Submit">
</form>
PHP 形式:
$query = ("SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");
$result=mysql_query($query);
if($result)
{
if(mysql_num_rows($result) >= 1)
{
echo "<script type='text/javascript'>alert('User already exist'); location.href = '../admin_home.php';</script>";
}
}
else{
$sql="INSERT INTO students (studentnumber, college, course, status, lname, fname, mname, address, gender)
VALUES
('$_POST[studentnumber]','$_POST[college]','$_POST[course]','$_POST[status]','$_POST[lname]','$_POST[fname]','$_POST[mname]','$_POST[address]','$_POST[gender]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<script type='text/javascript'>alert('Record Successfully Added'); location.href = '../admin_home.php';</script>";
}
我不知道为什么,但我总是得到未定义的索引错误。也许我在某个地方做错了什么。谢谢!!
【问题讨论】:
-
首先 - 阅读“SQL 注入” - 你的代码不安全。
-
错误的行号会有所帮助
-
写入完整错误
-
注意:未定义索引:第 11 行 C:\xampp\htdocs\SIS\WEW\queries\insert.php 中的学生编号这是错误,第 11 行将是“$query = (” SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");"