简体   繁体   English

如何从 PHP 中的 mySQL 表填充下拉框

[英]How to Populate a Drop down box from a mySQL table in PHP

I have a mysql database with a drivers table which has the following fields: id forename surname nationality team_id我有一个带有驱动程序表的 mysql 数据库,其中包含以下字段:id forename surname nationality team_id

I am trying to create a PHP which retrieves the names and id of each driver and then the output should contain an HTML form which should contain a submit button and a Drop-down box.我正在尝试创建一个 PHP,它检索每个驱动程序的名称和 ID,然后输出应包含一个 HTML 表单,该表单应包含一个提交按钮和一个下拉框。 The drop-down input should contain the driver names, and the form should submit via the GET method to another php named task4.php when the submit button is pressed.下拉输入应包含驱动程序名称,当按下提交按钮时,表单应通过 GET 方法提交到另一个名为 task4.php 的 php。

I have started doing some of the code but unable to finish it.我已经开始做一些代码,但无法完成。

<?php
// Create connection
$con=mysqli_connect("hostname","login","password","db_name");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers);

$options="";

while ($row=mysql_fetch_array($result)) {
$id=$row["forename"];
$thing=$row["surname"];
$options.="OPTION VALUE=\"$id\">".$forename;
}
?>

Please Help Me请帮我

When you get a result from the SELECT statement, it comes in the form $row['columnName'] .当您从SELECT语句中获得结果时,它以$row['columnName']

$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers");

$options="<select>";

while ($row=mysqli_fetch_array($result)) {

    $options .= '<option value="'.$row["id"].'">'.$row["forename"].' '.$row["surname"].'</option>';

}

$options .= '</select>';

echo '<form action="task4.php">';
echo $options;
echo '<input type="submit" name="submit">';
echo '</form>';
<?php
// Create connection
$con = mysqli_connect("hostname","login","password","db_name");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$query = 'SELECT id, forename, surname FROM Drivers';

if($result = mysqli_query($con, $query))
{

    $options="";

    while ($row=mysqli_fetch_array($result)) 
    {
        $id=$row['id'];
        $surname=$row['surname'];
        $forename = $row['forename'];
        $options.="<option value='$id'>" . $forename . ' ' . $surname . '</option>';
    }
    echo '<form name="form1" action="task4.php" method="get">';
        echo '<select name="dropdown">';
            echo $options;
        echo '</select>'</br>;
        echo '<input type="submit" value="Send">';
    echo '</form>';
}
?>

This should give you start.这应该让你开始。

<?php
// Create connection
$con=mysqli_connect("hostname","login","password","db_name");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL:" . mysqli_connect_error();
}    

$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers);
?>

<form action='form4.php' method='get'>
<select name='driver_name'>

<?php
while ($row=mysqli_fetch_array($result)) {
echo "<option value='{$row['id']}'>".$row['forename']." ".$row['lastname']."</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM