[英]Deleting from table using drop down list
I have a problem with deleting from mySQL table.我在从 mySQL 表中删除时遇到问题。 I'm using drop down list to select which name (id) I need to delete.
我正在使用下拉列表来选择我需要删除的名称 (id)。 Please help.
请帮忙。
<h1><a href="#" class = "2">Delete product</a></h1> <form method="post" action = "Delete.php"> <div class="Delete"> <select> <?php require('connect.php'); $query = mysql_query("SELECT name FROM `products`"); $id = mysql_query("SELECT id FROM `products`"); while($row=mysql_fetch_array($query)){ echo "<option value='". $row = $_POST['id']."'>".$row['name'].'</option>'; } ?> </select> <input type="submit" name="" value="Delete"> </form> </div>
And this is script.这是脚本。 It makes error on line 10 -
if(isset($_POST['id'])){
它在第 10 行出错 -
if(isset($_POST['id'])){
<?php if($_SERVER["REQUEST_METHOD"] == "POST"){ require('connect.php'); $id = mysql_query("SELECT id FROM `products`"); if(isset($_POST['id'])){ $id = mysql_real_escape_string($_POST['id']); $query2 = "DELETE FROM `products` WHERE id = '$id'"; $result=mysql_query($query2); if($result){ header("Location: tools.php"); exit; } else{ echo"ERROR"; } } else{ echo"Bad ID"; } } ?>
Try something like this尝试这样的事情
//Give select a name so delete.php can hook into it
<select name="product_id">
<?php
require('connect.php');
//Merge your 2 queries into one
$query = mysql_query("SELECT id, name FROM products");
//Fix value fetching in your while loop
while($row=mysql_fetch_array($query)){
echo "<option value='". $row['id']."'>".$row['name'].'</option>';
}
?>
</select>
Then in your submit script然后在您的提交脚本中
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
require('connect.php');
//Look for select name
if(isset($_POST['product_id'])){
//Get ID from select value
$id = mysql_real_escape_string($_POST['product_id']);
$query = "DELETE FROM products WHERE id = '$id'";
$result = mysql_query($query);
if($result){
header("Location: tools.php");
exit;
}
else{
echo"ERROR";
}
}
else{
echo"Bad ID";
}
}
?>
I havnt tested this but with minor tweaking if any, it should now work for you.我还没有测试过这个,但如果有的话,只要稍微调整一下,它现在应该适合你。
What i have done我做了什么
Hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.