Remove button code isn't working for me???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Remove button code isn't working for me???

Post by cturner »

I am trying to build a php program for a client of mine. The program is suppose to add an image name with the extension to a database. When the image is no longer needed the user removes the image from the database. Anyway my code below will submit the image but removes all of the images at once. Is there a better way of doing this? Can someone please help me sort out removing a selected image from the database? Thanks in advance.

Here is the some php code:

Code: Select all

require "config.php";
$findphoto = $_FILES['findphoto'];
$photo = mysql_real_escape_string($_POST['photo']);
$id = mysql_real_escape_string($_GET['id']); // get the id number from the url
$id = mysql_real_escape_string($_POST['id']); // get the id number from the hidden field
$arrErrors = array();
if (isset($_POST['btnsubmit'])) {
	if($_FILES['findphoto']['name'] == '') {
       $arrErrors['findphoto'] = 'You did not select a photo to upload';
    }
	if ($photo == '') {
		$arrErrors['photo'] = 'Please enter a photo name and file extension that you wish to upload for this clearing sale.';
	}	
	if (count($arrErrors) == 0) {
		$query = mysql_query("SELECT `id` FROM `clearingsales` WHERE `id` = '$id'") or die("Could not query because:" .mysql_error());
		$row = mysql_fetch_array($query);
		$parent_id = $row['id'];
		$insert = "INSERT INTO `clearingsales_photos` (`parent_id`, `photos`) VALUES ('$parent_id', '$photo')";
		if (mysql_query ($insert)) {
			print "<strong>Photo has been added to the database. Please don't forget to upload the photos via ftp.</strong><br /><br />";
		} else {
			print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $insert.</p>";
		}
	} else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ul></div>';
	}
}
if (isset($_POST['removebtn'])) {
$query2 = mysql_query("SELECT * FROM clearingsales_photos WHERE parent_id = '$id'") or  die ("Could not query because:" .mysql_error());
$row2 = mysql_fetch_assoc($query2);
$parent_id = $row2['parent_id'];
$delete = "DELETE FROM clearingsales_photos WHERE parent_id = '$parent_id'";
if (mysql_query ($delete)) {
	print "<strong>Photo has been deleted from the database.</strong><br /><br />";
} else {
	print "<p>Could not delete the entry because: <b>" . mysql_error() . "</b>. The query was $delete.</p>";
}
/*("SELECT parent_id, photos FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_array($select);
	echo $select;
    $parent_id = $r['parent_id'];
$query = "DELETE FROM clearingsales_photos WHERE parent_id = '$parent_id'";
echo $query;
mysql_query($query );
   if ($query) {
      print "<strong>Photo has been deleted</strong>.";
  } else {
    print "Could not delete the entry because: " . mysql_error() . ". The query was ".$query;
   }*/
}
	echo $strError;
Here is the html code with some php code:

Code: Select all

<strong>Select Photos</strong><br />
<form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="attachForm" id="attachForm">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Before selecting the photo please make sure the photo size is 100 x 100 and make sure the photo's file extension is a jpg.<br /><br />
Please find the product photo then copy the file name and extension only then paste the file name in the textbox below. After you have done that you will need to upload the file via ftp.
  <p<?php if (!empty($arrErrors['findphoto'])) echo ' class="formerror"'; ?>><input type="file" name="findphoto" value="<?php echo $findphoto; ?>" />
  <br />
  <p<?php if (!empty($arrErrors['photo'])) echo ' class="formerror"'; ?>><input type="text" name="photo" value="<?php echo $photo; ?>" />
  <br />
  <input name="btnsubmit" type="submit" id="btnsubmit" value="SUBMIT" />
  <br />
  <br />
</form>
<strong>Current Photos</strong><br />
<form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="removeForm" id="removeForm">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php
$query2 = mysql_query("SELECT parent_id, photos FROM `clearingsales_photos` WHERE `parent_id` = '$id' ORDER BY photos") or die("Could not query because".mysql_error());
while ($row2 = mysql_fetch_array($query2)) {
	echo $row2['photos']."<input name=removebtn type=submit id=removebtn value=REMOVE /><br />";
}
if (mysql_affected_rows() == 0) {
	echo "No photos have been added to the database.";
}
?><br />
</form>
<?php
mysql_close();
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Remove button code isn't working for me???

Post by onion2k »

cturner wrote:Anyway my code below will submit the image but removes all of the images at once. Is there a better way of doing this?
You're deleting all the images with a particular parent_id. You need to give each image it's own id in the clearingsales_photos table, and then use that to delete them individually. Alternatively, pass the value of 'photos' back from the form and use that to delete the image.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I am sorry but it is too early for me to think and I must solve this problem as soon as possible. I am in Australian eastern time.

How am I suppose to create the photo's id and pass that value back from the form?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

cturner wrote:I am sorry but it is too early for me to think and I must solve this problem as soon as possible. I am in Australian eastern time.
That sounds like a recipe for disaster. You need to take time to plan your application properly. Rushing in and hacking in a solution will just lead to bigger problems later. Potentially very serious unsolvable ones that break your code irrepairably and mean you need to recode most of it. That's the sort of problem that takes a business from profitable to bankrupt. Needlesstosay, you don't need to solve this "as soon as possible", you need to solve it "correctly".
cturner wrote:How am I suppose to create the photo's id and pass that value back from the form?
There's lots of solutions. I'd add an autoincrementing int column to the table and use that value instead of your current 'parent_id' when you're deleting an image.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Sweet but how do I get the autoincrement id from the database? Is it possible to show me some code.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You'd get the id out of the database in the same way you're currently getting the parent_id.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Now I know this is the way to retrieve the photos_id:

Code: Select all

"SELECT photos_id FROM clearingsales_photos WHERE photos_id = 'WHERE'";
but where WHERE is I am not sure of what to put there and if it is a variable where it should come from. Please help me.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well using auto_increment, the "WHERE" is the easy part. Depending on how you decide which get's deleted, you'd list links/options for the user to select which to delete, thus giving you the unique id.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

The user will have to press one of many remove buttons to delete the image. For example:
image1.jpg REMOVE
image2.jpg REMOVE
image3.jpg REMOVE
There will be a textbox to find a image, and there will be another textbox to place the image's name and file extension. With a submit button underneath both textboxes.

Can you please show me some code?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Wait... You want them to type in a name to find the image to be deleted, or you want the image to be in a list with remove/delete buttons next to the names?
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

superdezign wrote:Wait... You want them to type in a name to find the image to be deleted, or you want the image to be in a list with remove/delete buttons next to the names?
I want the image to be in a list with remove/delete buttons next to the names.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Don't worry I have worked out a better way of doing this.
Post Reply