Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:21:41+00:00 2026-05-26T22:21:41+00:00

I have an image upload form and it works fine. But underneath that same

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:21:42+00:00Added an answer on May 26, 2026 at 10:21 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that performs some simple editing which works fine, the problem
I have an application that uses the Paperclip plugin for image upload. Now that
Let's say i have an image uploader script, i want to prevent the upload
How to do the following: I want to have a link [upload image] which
We have a webapp where people can upload various image file types and on
I have a p:fileUpload which works fine. But I need a partial page rendering
I have a simple upload form working in PHP (works in web) and also
I have a WCF restful service that I'm trying to upload an image to.
I have an image upload form <% using (Html.BeginForm(PictureValidateAndSave, UserGallery, new {}, FormMethod.Post, new
I have an image upload page (single page). When submitted it check filesize and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.