带有MySQL形式的HTML下拉框

时间:2013-06-05 18:18:04

标签: mysql

我有PHP编码,但我没有把它转移到HTML表单。有什么建议吗?

<?php
$db_host =  "localhost";
$db_username  =  "combsb_combsb";
$db_pass =  "pat60086";
$db_name =  "combsb_sample";

@mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
@mysql_select_db("$db_name") or die ("No Database");

Echo"Successful Connection";


$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);

echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";


?>

1 个答案:

答案 0 :(得分:0)

首先,我要确保在PHP周围添加HTML样板文件。然后我认为你的意思是mysql_fetch_row而不是mysql_fetch_array。您的最终文件应如下所示:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
<?php
// *Cut out for brevity, remember to paste back in* 
$result = mysql_query($sql);
?>

    <form action="" method="POST">

<?php
echo "<select name='compname'>";

while ($row = mysql_fetch_row($result)) {
  echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}

echo "</select>";
?>

    </form>
  </body>
</html>

下次发布您的输出/错误或者如果这不起作用