我有一个表单,它在提交时将数据发送到外部 .php 脚本。我想禁用将 2 个同名学生插入数据库的可能性。因此,如果输入无效,我希望表单中的输入字段具有最后输入的值。我解决了这个问题,但我不知道如何检索最后选择的选项?这是我的代码:
<form action="student.php" name="add-student" method="post" onSubmit="return Validate()">
Firstname:<input type="text" name="firstname" value="<?php if(isset($_GET['firstname'])){echo $_GET['firstname'];}
Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option value=$_GET[["degree"]></option>';} ?>"> **//This is what i can't solve!!!**
<option value="1">First degree</option>
<option value="2">Second degree</option>
<option value="3">Third degree</option>
</select>
</form>
我的外部 php 脚本:
<?php
$firtsname=$_POST['firstname'];
$degree=$_POST['degree'];
$duplicate=mysqli_query($con,"SELECT * FROM student WHERE firstname='$firstname'");
if(mysqli_num_rows($duplicate)>0)
{
$row = mysqli_fetch_assoc($duplicate);
echo '<script type="text/javascript">';
echo 'alert("Name already exists in db.")';
echo '</script>';
echo "<script>window.location.assign('http://localhost/administrator.php?firstname=".$firstname."°ree=".$degree."'); </script>";
}
else{
$sql="INSERT INTO student (firstname,degree) VALUES('$firstname','$degree')";
?>