donglang6656 2011-11-11 09:22
浏览 48
已采纳

从数据库中删除特定图像[simple php / mySql]

I have an image upload form and it works fine.

But underneath that same form I want some sort of image listing where I can delete specific image by clicking on its X button. I wrote the code and it works but it always deletes the first image on the list, no matter which X I click on.

<form method="post">
            <ul>
                <?php 

                $host = "127.0.0.1"; //database location
                $user = ""; //database username
                $pass = ""; //database password
                $db_name = ""; //database name

                if(!$link = mysql_connect($host, $user, $pass)) {
                    echo "<p>error: ".mysql_error()."</p>";
                } else {
                    mysql_select_db($db_name);
                }

                $selectAll = "SELECT image_name FROM home_images";
                $doIt = mysql_query($selectAll);

                // if(isset($_POST['delete'])) {
                //     mysql_query("DELETE FROM home_images WHERE image_name = ");
                // }

                ?>
                <?php while($result = mysql_fetch_array($doIt)) : ?>

                    <li style="display:inline; margin-right:10px">
                        <img src="<?php bloginfo('url') ?>/wp-content/uploads/<?php echo $result[0]; ?>" height=50 width=60 />
                        <input type="hidden" value="<?php echo $result[0]; ?>" name="imagename" />
                        <input type="submit" value="X" name="delete" />
                    </li>

                <?php endwhile; ?>

                <?php

                    if(isset($_POST['delete'])) {
                        $imagename = $_POST['imagename'];
                        $deleter = "DELETE FROM home_images WHERE image_name = '$imagename'";
                        if(mysql_query($deleter)) {
                            echo "Successful!";
                            echo $imagename;
                        } else {
                            echo mysql_error();
                        }
                    }
                ?>



            </ul>
            </form>

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • dougu2036 2011-11-11 09:27
    关注

    The problem is that you put everything inside the same form. When the form has multiple fields with the same name it sends only one of them.

    Make a separate form for each delete button:

    <?php while($result = mysql_fetch_array($doIt)) : ?>
        <li style="display:inline; margin-right:10px">
            <form method="post">
                <img src="<?php bloginfo('url') ?>/wp-content/uploads/<?php echo $result[0]; ?>" height=50 width=60 />
                <input type="hidden" value="<?php echo $result[0]; ?>" name="imagename" />
                <input type="submit" value="X" name="delete" />
            </form>
        </li>
    <?php endwhile; ?>
    

    By the way, you have a huge security issue with the SQL query. Always sanitize incoming data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?