使用mySQL创建下拉列表但收到错误

时间:2014-04-03 19:22:02

标签: php

所以基本上我有一个数据库,里面有一个名为drivers的表,结构如下:

驱动程序: ID 用的名字 姓 国籍 team_id

现在我想在HTML表单中提取Forename和Surname。我需要一个提交按钮和一个下拉列表输入。下拉列表将包含驱动程序的名称,表单将通过GET方法提交。

我怎么可能这样做?这是我到目前为止,但它似乎没有工作,我没有

<?php
$con=mysqli_connect("hostname","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Write out our query.
$link=mysqli_connect(hostname,username,password,dbname));
$result = mysqli_query( "SELECT * FROM Drivers");
// Execute it, or return the error message if there's a problem.
$result = mysqli_query($query) or die(mysqli_error());

$dropdown = "<select name='Drivers'>";
while($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<option value='{$row['forename']}'>{$row['surname']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;

&GT;

我对PHP很陌生,所以对这样一个noob问题道歉。

1 个答案:

答案 0 :(得分:1)

试试这个

<form action="" method="get">
    <?php
    $con=mysqli_connect("hostname","username","password","dbname");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // Write out our query.
    $link=mysqli_connect(hostname,username,password,dbname);
    $result = mysqli_query("SELECT * FROM Drivers");
    // Execute it, or return the error message if there's a problem.
    $result = mysqli_query($query) or die(mysqli_error());

    $dropdown = "<select name='Drivers'>";
    while($row = mysql_fetch_assoc($result)) {
        $dropdown .= "\r\n<option value='{$row['forename']}'>{$row['surname']}</option>";
    }
    $dropdown = "\r\n</select>";
    echo $dropdown;
?>

<input type="submit" value="Send">
</form>

问题来自这里:

$dropdown .= "\r\n</select>";

<强>固定