我可以使用以下功能删除记录吗?如果是,我该怎么做?我必须将该功能传递给提交按钮?
function deleteImage($id = "")
{
if(isset($_POST['delete_image']))
{
$query = mysql_query("DELETE FROM store WHERE id='$id'");
}
return $query;
}
html看起来像这样:
<?php
$images = mysql_query("SELECT * FROM store");
while($row = mysql_fetch_assoc($images)) {
?>
<form action="admin.php" method="post">
<?php
echo "<img src=".$row['image'].">";
?>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
<?php
}
?>
提前致谢!
答案 0 :(得分:2)
您可以执行以下操作,在循环中添加表单中的隐藏输入。
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"
然后在action.php文件中
$id = $_POST['id'];
if(isset($_POST['delete_image']))
{
$query = mysql_query("DELETE FROM store WHERE id='$id'");
}
答案 1 :(得分:1)
你应该试试这个。
<?php
if(isset($_POST['delete_image']))
{
$query = mysqli_query("DELETE FROM store WHERE id='".$_POST['image_id']."'")or die(mysqli_error());
}
$images = mysqli_query("SELECT * FROM store");
while($row = mysqli_fetch_assoc($images))
{
?>
<form action="admin.php" method="post">
<?php echo "<img src=".$row['image'].">"; ?>
<input type="hidden" id="image_id_<?php echo $row['id']; ?>" value="<?php echo $row['id']; ?>" name="image_id"/>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
<?php
}
?>
我在隐藏字段中传递了图片ID。
答案 2 :(得分:0)
这是您的观点
<td>
<?php $images = mysql_query("SELECT * FROM store"); ?>
<form action="index.php" method="post">
<?php while($row = mysql_fetch_assoc($images)): ?>
<img src="<?php echo $row['image']; ?>">
<input type="hidden" name="deleting[]" value="<?php echo $row['id_from_database'] ?>">
<?php endwhile; ?>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
</td>
这是您的代码
<?php
function deleteImage()
{
if(isset($_POST['delete_image'], $_POST['deleting']) && !empty($_POST['deleting']))
{
$ids = array_map('intval', $_POST['deleting']); // Type conversion to integer
$ids = implode(', ', $ids); // Bringing to id1,id2...
$query = mysql_query('DELETE FROM images WHERE id IN(' . $ids . ')'); // Deleting
return (bool)$query;
}
}
?>
现在您需要设置要删除的图像的复选框,然后按提交。