我是一名新手并正在为学校项目工作
我有一个列出食物的网站。
我有一个更新表,允许我更改和添加数据。
对于食物组字段,我将它与另一个名为food_group的表交叉引用,该表具有food_group(名称)和id。
当您查看食物数据时,您可以看到它所取的名称而不是ID。在更新页面上,我想要下拉到ID的位置。因此,您可以看到“友好”名称而不是ID号,但它必须在食物表中存储ID而不是友好名称。
网站可在http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
找到我的代码是:
<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);
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>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></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>";
mysql_close($con);
?>
</body>
</html>
的 的 __ _ __ _ __ _ __ _ 更新12/2/13 10:30 pm _ __ _ __ _ __ < EM> _ __ _ __ _ __ _ 好的,如果我创建一个新的PHP页面,如下所示它将工作。但是,我不知道如何将它组合到原来的上面......有人可以帮忙吗?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
感谢您的帮助! 杰森
答案 0 :(得分:1)
你的脚本很好,但我只想指出以下内容:
没有必要连接这个
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
你可以这样输入:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
你也错过了关闭输入标签
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
另一个是你需要引用你的属性值,见下文
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
最后一件事是你对SQL注入开放,所以你应该开始学习mysqli和准备好的语句