我有一个表单,提交时将数据发送到外部.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')";
?>
答案 0 :(得分:0)
更改为
Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option
value='".$_GET["degree"]."' selected></option>';} ?>">
答案 1 :(得分:0)
错误地使用输入和下拉表单。试试这个
<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'];}?>">
</form>
Degree:<select name="degree">
<option value="<?php if(isset($_GET['degree'])){echo $_GET["degree"];}?>"></option>
<option value="1">First degree</option>
<option value="2">Second degree</option>
<option value="3">Third degree</option>
</select>
答案 2 :(得分:0)
行情-问题。这一行
<?php if(isset($_GET['degree'])){echo '<option value=$_GET[["degree"]></option>';} ?>
应该是 -
<?php if(isset($_GET['degree'])){ $v = $_GET['degree']; echo '<option value="$v"></option>';} ?>
注意强>
"$foo"
)中访问变量。$foo
')内访问变量。它将是一个值为$ foo的字符串。 ('$foo'=="\$foo"
)。更新 - 您的标记格式不正确。
<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option value=$_GET[["degree"]></option>';} ?>">
实际上会打印像 -
<select name="degree" value="<option value=x</option>"> <!-- Wrong! -->
试试这个 -
<select name="degree">
<?php if(isset($_GET['degree'])){$deg = $_GET["degree"]; echo "<option selected="selected" value=\"$deg\"></option>";} ?>">
答案 3 :(得分:0)
当数据库能够工作时,让它完成工作。在您的情况下,您将PHP用于大部分实际的数据库操作。
我会这样做:(我没有测试过)
<?php
$firstname=$_POST['firstname'];
$degree=$_POST['degree'];
$stmt = $mysqli->prepare("INSERT IGNORE INTO student (firstname,degree) VALUES(?, ?)");
$stmt->bind_param($firstname, $degree);
$result = $stmt->execute();
$lastInsertID = $stmt->insert_id;
//lastInsertID would be 0 if no execution of INSERT has been made (based on the IGNORE keyword)
if($lastInsertID == 0)
{
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>";
}
?>
要解决有关在错误时检索选项的问题,请将您的行更改为:
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>';} ?>">
还要将重定向更改为:
echo "<script type=\"text/javascript\">window.location.assign('http://localhost/administrator.php?firstname='.$firstname.'°ree='.$degree); </script>";
答案 4 :(得分:0)
我解决了这个问题,无论如何,谢谢大家的帮助,它应该是这样的:
Degree:<select name="degree">
<option value="1" <?php if($_GET['degree']=='1') {?> selected="selected" <?php }; ?> >First degree</option>
<option value="2" <?php if($_GET['degree']=='2') {?> selected="selected" <?php }; ?> >Second degree degree</option>
<select>