0

我有一个表单,它在提交时将数据发送到外部 .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."&degree=".$degree."'); </script>";
}
else{
$sql="INSERT INTO student (firstname,degree) VALUES('$firstname','$degree')";
?>
4

5 回答 5

0

改成这个

   Degree:<select name="degree" value="<?php if(isset($_GET['degree'])){echo '<option
    value='".$_GET["degree"]."' selected></option>';} ?>">
于 2013-03-30T11:49:58.673 回答
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>
于 2013-03-30T12:10:06.677 回答
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>";} ?>">
于 2013-03-30T12:18:25.847 回答
0

当数据库能够工作时,让它完成工作。在您的情况下,您将 PHP 用于大部分实际的数据库操作。

  1. 确保数据库中的 firstname 列的键设置为唯一。
  2. 在查询中使用参数(用于避免 sql 注入)

我会做这样的事情:(我没有测试过)

<?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."&degree=".$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.'&degree='.$degree); </script>";
于 2013-03-30T12:57:49.750 回答
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>
于 2013-11-10T20:49:23.053 回答