Msmit1993
Msmit1993

Reputation: 1710

Delete records in mysql database with query in link

I have a MySQL database and with a php function i made a table is created to view the content. Now i have an image (a little red cross). Now i want that if the user click's on the image a delete query is executed. But i don't know how.

$column = array_keys($array[0]);

 echo "<table border='1'>";
            echo "<tr>";
            foreach($column as $title)
            {

                echo "<td width='100px'>$title</td>";

            }
            echo "</tr>";

            echo "<tr>";
            foreach($array as $row)
            {
                foreach($row as $item)
                {
                    echo "<td>$item</td>";
                }

                 echo '<td><img src="delete_button.png value="'.$row['id'].'"" /></td>'; //the button and the id as value
                echo "</tr>";
            }

            echo "</table>";

Upvotes: 0

Views: 2740

Answers (3)

NaturalBornCamper
NaturalBornCamper

Reputation: 3866

You would have to put a link on the image as such

echo '<td><a href="delete.php?id='.$row['id'].'"><img src="delete_button.png" /></a></td>';

And then in your delete.php file or whatever other script you put, you retrieve the id with

$_GET["id"]

And you execute your query.

I hope your site is secured and that not everyone or every crawler and hit that link though, make sure it is safe, and I would recommend adding a confirm box at the very least.

Upvotes: 3

Uday Sawant
Uday Sawant

Reputation: 5798

Use ajax call to onClick event of image. get a confirmation from user and then make a call to php function that deletes the required row. You can also display a confirmation windows based on return value of ajax call.
Check this link for details on jQuery ajax call http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Ravinder Singh
Ravinder Singh

Reputation: 3133

Put the delete button image in an anchor tag and make it a link and just call a javascript function to confirm that if user really wants to delete the data and if he agrees then send the user to a php page where you will write the code to delete the data. That's it.

Upvotes: 0

Related Questions