PHP表单,将输入字段转换为下拉列表


PHP form, converting input field into a drop down list

下面的代码是一个简单的表单,它将输入的数据发送到本地数据库。

<html>
    <head>
        <title>!!!!!!!!!!!!!!</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div id="main">
            <h1>Insert data into database using mysqli</h1>
            <div id="login">
                <h2>Student's Form</h2>
                <hr/>
                <form action="" method="post">
                    <label>Student Name  :</label>
                    <input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
                    <label>Student Email  :</label>
                    <input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
                    <label>Student City  :</label>
                    <input type="text" name="stu_city" id="school"   required="required" placeholder="Please Enter Your City"/><br/><br />
                    <input type="submit" value=" Submit " name="submit"/><br />
                </form>
            </div>
        <!-- Right side div -->
        </div>
        <?php
        if(isset($_POST["submit"])){
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "Student";
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 
            $sql = "INSERT INTO students (student_name, student_email, student_school)
            VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
            if ($conn->query($sql) === TRUE) {
                echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
            } else {
            echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
            }
            $conn->close();
        }
        ?>
    </body>
</html>

我发现的问题是,我试图将Student City输入字段更改为从数据库检索值的下拉列表,并将其放入下拉列表中供新用户选择。

有人能告诉我需要做什么吗?

我正在尝试使用下面的代码,并将下面的列表发送到我的数据库。

       <option value="US">United States</option>
        <option value="UK">United Kingdom</option>
        <option value="France">France</option>
        <option value="Mexico">Mexico</option>

但我发现很难将这些值发送到数据库与我的上述代码以及在哪里放置此代码。

作为如何使用数据库中的数据构建下拉菜单的粗略示例,这可能会给您一个大致的概念。

/* store formatted menu options in temp array */
$html=array();
/* query db to find schools/cities */
$sql='select distinct `student_school` from `students` order by `student_school`';
$res=$mysqli_query( $conn, $sql );
/* process recordset and store options */
if( $res ){
    while( $rs=mysqli_fetch_object( $res ) ){
        $html[]="<option value='{$rs->student_school}'>{$rs->student_school}";
    }
}
/* render menu */
echo "<select name='stu_city'>", implode( PHP_EOL, $html ), "</select>";

您需要通过将if (isset($_POST))移动到html:

之上来重构您的代码。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
// Create connection
$conn = new mysqli ( $servername, $username, $password, $dbname );
// Check connection
if ($conn->connect_error) {
    die ( "Connection failed: " . $conn->connect_error );
}

$sql = "SELECT city_name FROM cities" ;
if ($conn->query ( $sql ) === TRUE) {
    $cities = ... // build the cities from the query result 
} else {
    $cities = '<option value="none">No cities found</option>' ;
}

if (isset ( $_POST ["submit"] )) {
    $sql = "INSERT INTO students (student_name, student_email, student_school)
VALUES ('" . $_POST ["stu_name"] . "','" . $_POST ["stu_email"] . "','" . $_POST ["stu_city"] . "')";
    if ($conn->query ( $sql ) === TRUE) {
        echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
    } else {
        echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error . "');</script>";
    }
    $conn->close ();
}
?>

<html>
<head>
<title>!!!!!!!!!!!!!!</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="main">
        <h1>Insert data into database using mysqli</h1>
        <div id="login">
            <h2>Student's Form</h2>
            <hr />
            <form action="" method="post">
                <label>Student Name :</label> <input type="text" name="stu_name"
                    id="name" required="required" placeholder="Please Enter Name" /><br />
                <br /> <label>Student Email :</label> <input type="email"
                    name="stu_email" id="email" required="required"
                    placeholder="john123@gmail.com" /><br />
                <br /> <label>Student City :</label> <select name="stu_city" multiple><?php echo $cities; ?>
                </select>><br />
                <br /> <input type="submit" value=" Submit " name="submit" /><br />
            </form>
        </div>
        <!-- Right side div -->

    </div>
</body>
</html>

使用Select标签:假设你的数据库中有一列Student City,就像这样,假设数据库字段名为City

  1. 城市1
  2. 都市2
  3. 城市
  4. 3

步骤1:查询数据库并获取所有城市

    $sql = "SELECT city FROM table_name";
    $result = $conn->query($sql);

然后你来到你的下拉菜单:

    <select name="stu_city" id="..." required>
      <?php
      while($cities = $conn->fetch_array($result){
        extract($cities);
        echo "<option value='...'>$city</option>";
      }
      ?>
    </select>

您需要通过将if (isset($_POST))移动到html:

之上来重构您的代码。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
// Create connection
$conn = new mysqli ( $servername, $username, $password, $dbname );
// Check connection
if ($conn->connect_error) {
    die ( "Connection failed: " . $conn->connect_error );
}

$sql = "SELECT city_name FROM cities" ;
$result = $conn->query ( $sql );

if (isset ( $_POST ["submit"] )) {
    $sql = "INSERT INTO students (student_name, student_email, student_school)
VALUES ('" . $_POST ["stu_name"] . "','" . $_POST ["stu_email"] . "','" . $_POST ["stu_city"] . "')";
    if ($conn->query ( $sql ) === TRUE) {
        echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
    } else {
        echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error . "');</script>";
    }
    $conn->close ();
}
?>

<html>
<head>
<title>!!!!!!!!!!!!!!</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="main">
        <h1>Insert data into database using mysqli</h1>
        <div id="login">
            <h2>Student's Form</h2>
            <hr />
            <form action="" method="post">
                <label>Student Name :</label> <input type="text" name="stu_name"
                    id="name" required="required" placeholder="Please Enter Name" /><br />
                <br /> <label>Student Email :</label> <input type="email"
                    name="stu_email" id="email" required="required"
                    placeholder="john123@gmail.com" /><br />
                <br /> <label>Student City :</label> <select name="stu_city" multiple> 
<?php
if ($result == TRUE) {
    while($cities = $conn->fetch_array($result)){
      extract($cities);
      echo "<option value=''>$city_name</option>";
 } 
}
else {
    echo "<option value='none'>No cities found</option>";
}
?>
                </select>><br />
                <br /> <input type="submit" value=" Submit " name="submit" /><br />
            </form>
        </div>
        <!-- Right side div -->

    </div>
</body>
</html>