Remove button code isn't working for me???
Posted: Tue Jan 30, 2007 4:06 am
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:
Here is the html code with some php code:
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;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();