0

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?

1
  • Try checking what $imagename is right before the SQL query that deletes and make sure it is correct for each picture. Commented Nov 11, 2011 at 9:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In the OP's case, would it not make sense that it would delete the last picture instead of the first because wouldn't PHP retain only the last value of multiple inputs with the same name?
Thanks, exactly what I needed. I agree it's terrible security-wise, but this is just simple testing, I was stuck with this for a while.
1

If you want a non-javascript solution, you could do the multiple form thing Juhana suggested. Or if you want a single form, then you could add a radio button (or even checkboxes for multiple selections) under every image and then have a single submit button to delete your selection(s).

            <?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 />
                    <div <!--some kind of formatting here i suppose-->>
                    <input type="radio" value="<?php echo $result[0]; ?>" name="imagename" />
                    </div>
                </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();
                    }
                }
            ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.