Forum Moderators: coopster

Message Too Old, No Replies

Help with deleting a record from MySQL

         

chaddsisco

1:17 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



The page below works fine untill the part when the user hits the "submit" button to delete the picture, I have tried a few diffrent ways of doing this and I cant seem to figure it out, any ideas? Thanks in advance for your help!

<?php
//connect to the database
$link = mysql_connect("localhost", "admin", "password")
or die("Could not connect: " . mysql_error());
mysql_select_db("gallery", $link)
or die (mysql_error());

$folder_location = $_POST['folder_location'];
$picture_id = $_POST['picture_id'];
$ImageDir = $_POST['ImageDir'];
$ImageThumb = $_POST['ImageThumb'];
$slash = "/";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Delete Image Confirm</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
echo "folder location: <b>$folder_location</b> and picture id: <b>$picture_id</b>";
echo "Image Dir: <b>$ImageDir</b> and ImageThumb: <b>$ImageThumb</b>";
?>

<p align="center">Listing of picture to be deleted</p>
<table align="center">

<?php
$ImageDir = $ImageDir .$slash;
//tell database which folder to display
switch ($folder_location) {
case "centerpieces":
$getpic = mysql_query("SELECT * FROM gallery_centerpieces WHERE image_id=$picture_id")
or die(mysql_error());
while ($rows = mysql_fetch_array($getpic)) {
extract($rows);
echo "<tr>\n";
echo "<td><a href=\"".$ImageDir . $image_id . ".jpg\">";
echo "<img src=\"" . $ImageDir . $image_id . ".jpg\" border=\"0\">";
echo "</a></td>\n";
echo "<td>" . $image_id . "</td>\n";
echo "</tr>\n";
}
break;
<removed a ton of other cases - jatar_k>
}
?>

</table>
<form name="form1" method="post" action="deleted_image_confirmed.php"
enctype="multipart/form-data">
<center><p>Are you sure you want to delete this picture from your gallery?</p>

</center>

<p align="center"><input type="submit" name="Submit" value="Submit">
&nbsp;
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>

</body>
</html>

[edited by: jatar_k at 3:17 pm (utc) on Sep. 13, 2005]

haggul

1:22 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Try rewriting as delete queries?..!

chaddsisco

1:42 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Thanks for the idea but I'm a little confused about what you mean, could you give me an example? Thanks

jatar_k

3:22 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you are deleting an image I don't see any code that is actually deleting anything

the form doesn't seem to contain any info about the image it is trying to delete and all the images just look like links around them to themselves.

you will need to have a way for the user to select an image, such as starting your form from the top of the page and having a radio button, or something, to enable them to select a certain image. That radio can have the image id or name as its value. That id will then get passed to the processing script on submit.

You can then use that id to issue a delete query to mysql.

Angelis

3:31 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



If you go into phpMyAdmin and delete the record it will actually dump the SQL code for it so you can rip it up and put it in the page so it works correctly.

If you cant be bothered doing that here is the php / sql.

<?php

$delete = mysq_query("DELETE $rowdata FROM $tablename WHERE $id = $what-you-want-to-delete");

?>

chaddsisco

4:52 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



Would I put that line of code in the "action" part of the form or some where else in the code? Thanks in advance for the help, Im still learning as I go.

chaddsisco

11:45 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



hmm I tried to do that in the "action" part of that form and could not get it to work right, but maybe I wrote the code wrong. Does anyone else have any ideas? Thanks

jatar_k

3:42 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it would go into the script that is referenced by the action portion of the form.

once you select an image and post its id to the next script. The script will then have to have a way to know you want to delete, if it is multifunctional, and what image it is you want to delete.

you would then use the id posted from the form and issue a delete query as Angelis showed.