dongnianwo8289 2015-10-09 17:36
浏览 23
已采纳

从数据库中删除PHP表单的问题

I'm trying to create a form in PHP that will delete a row in a MySQL database based on the ID of that row. For some reason the form doesn't seem to be passing the ID so it doesn't know what row to delete. This is my form:

<?php
//connect to the database//
include 'mysqlconnect.php';
?>

<?php $result = mysql_query("SELECT * FROM images"); ?>

<?php    
echo "<table border='1'>
<tr>
<th>Image ID</th>
<th>Image</th>
<th>Description</th>
<th>&nbsp;</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><img src='" . $row['image'] . "' width='300px' /></td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>
<form method='post' action='deleted.php'>
<input type='hidden' value='".$row['id']."' /> 
<input name='delete' type='submit' id='delete' value='Delete'>
</form>
</td>";
echo "</tr>";
}
echo "</table>"; // Close table
?>

The form data comes in, including the id. The form goes to deleted.php and here is the code on that page:

<?php
//connect to the database//
include 'mysqlconnect.php';
?>
<?php
$id = $_POST['id']; 
$query = "DELETE FROM images WHERE id = '".$id."'"; 

if(mysql_query($query)){ 
echo "deleted image ". $id;
//test to see if id is coming
echo $id;
} else{ 
echo "fail";
}
?>

This returns a success but the image is not deleted and the id does not come in. Any ideas?

  • 写回答

2条回答 默认 最新

  • dpx49470 2015-10-09 17:42
    关注

    You haven't named the input field that contains the id.

    This:

    <input type='hidden' value='".$row['id']."' />
    

    Needs to become this:

    <input type='hidden' name='id' value='".$row['id']."' /> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思