PHP / mySQL下拉框问题

时间:2013-12-03 21:19:45

标签: php html mysql html-select

我是php& mySQL的新手,这是一个学校项目。

我试图将一个下拉列表添加到food_group列但有问题。现在,我有下拉列表“工作”,但它只列出列表中的第一个项目,检索所有记录。不是他们目前所设定的。我不确定它是否正在进行中,但我正处于一个点,我看着我的代码并没有看到问题。您可以在http://web.nmsu.edu/~jrortiz/ICT458/FINAL/updateFood.php看到我正在谈论的页面以获得视觉效果。

我需要下拉菜单来显示food_group.food_group表中的友好名称。但它需要保存food_group.id值。因此,当我提取基本报告时,它会显示为:http://web.nmsu.edu/~jrortiz/ICT458/FINAL/food.php

我不确定我在这部分代码中做错了什么:(或者,如果它甚至在这部分中也是如此)

   while ($row = mysqli_fetch_array($myData2)) {
   $options.= '<option value="'.$row['food_group'].'">'.$row['food_group'].'</option>';
   };
       echo "<td><SELECT NAME=Food_group>";
       echo $options;
       echo "</SELECT></td>";

以下是完整的代码块:

<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}


if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]',         ph='$_POST[PH]' WHERE food='$_POST[hidden]'";              
mysql_query($UpdateQuery, $con);
};

if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";          
mysql_query($DeleteQuery, $con);
};

if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";        
mysql_query($AddQuery, $con);
};

$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);

$sql2="select * from food_group";
$myData2 = mysqli_query($con,$sql2) or die(mysql_error());

echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
   while ($row = mysqli_fetch_array($myData2)) {
   $options.= '<option value="'.$row['food_group'].'">'.$row['food_group'].'</option>';
   };
       echo "<td><SELECT NAME=Food_group>";
       echo $options;
       echo "</SELECT></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysqli_close($con);
?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以尝试这样做 - 回显while {}循环中的选择选项

echo "<td><SELECT NAME=Food_group>";    
  while ($row = mysqli_fetch_array($myData2)) {
       echo '<option value="'.$row['food_group'].'" $selected>'.$row['food_group'].'</option>';
       };
echo "</SELECT></td>";

然后为了保持当前选择的可见性,你只需要添加这样的东西(之前你回应选项)

    if ($row['food_group'] == 'whatever'){
        $selected = 'selected="selected"';
    } else { $selected = '';}

让我知道这是否有帮助:)