1

我可以使用以下功能删除记录吗?如果是,我该怎么做?我必须将功能传递给提交按钮吗?

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       
   }
?>

提前致谢!

4

3 回答 3

2

你可以做这样的事情,在 while 循环内的表单中添加隐藏的输入。

<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'");
}
于 2013-02-22T11:52:57.330 回答
1

You should try this.

<?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       
    }
?>

I passed image id in hidden field.

于 2013-02-22T11:49:48.550 回答
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;
    }
}
?>

现在您需要设置复选框要删除哪些图像并按提交。

于 2013-02-22T12:16:26.567 回答