Jump to content

My remove button code doesn't work???


cturner

Recommended Posts

As the subject says the code below doesn't remove the photo the database. Can someone please have a look at the code below and tell me why this is happening? Thanks in advance.

[code=php:0]
if (isset($_POST['removebtn'])) {
    $select = mysql_query("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
    $delete = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";
    if (mysql_query($delete)) {
        print "<strong>Photo has been deleted</strong>.<br />";
    } else {
        print "Could not delete the entry because: " . mysql_error() . ". The query was $delete.";
    }
}
[/code]
Link to comment
Share on other sites

[code]if (isset($_POST['removebtn'])) {
    $select = mysql_query("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
    $delete = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";
/*        start added code  */

$query=mysql_query($delete) or die(print "Could not delete the entry because: " . mysql_error() . ". The query was $delete.";)

/*      end added code    */

   
        print "<strong>Photo has been deleted</strong>.<br />";
 
       
    }
}[/code]

Try that.

I believe that using a mysql_query() statement inside an IF statement will always return false (I may be wrong, please feel free to correct me  ;D)

Are you seeing anything happen? Are you receiving errors?
Link to comment
Share on other sites

Try adding an
[code]echo 'Form has been submitted';[/code]
statement inside the if statement to ensure that the if statement is being processed.

If that echo's through, then the if statement is being ran. Then I would echo your queries and make sure that they are dropping in the variables that you are expecting.

echo your $parent_id and see if it is being substituted.

Go throught and make sure that each step is working and make sure that the line [code]if (isset($_POST['removebtn'])) {[/code]
is not returning false because that would produce a blank page.

Try something like this.
[code]
<?php
if (isset($_POST['removebtn'])) {

    $select = mysql_query("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
echo $parent_id; // this takes care of the verifying that the script is getting this far, and also tells you if the var is set properly
    $delete = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";

/*        start added code  */
$query=mysql_query($delete) or die(print "Could not delete the entry because: " . mysql_error() . ". The query was $delete.";)
/*      end added code    */

        print "<strong>Photo has been deleted</strong>.<br />";
    }
else{// this else statement is only for your own information, remove it before going live with the site.
echo 'Form variable could not be found';
}
?>
[/code]
Link to comment
Share on other sites

I am about to tear my hair out if I don't get a solution soon. Anyway I must find a solution before tomorrow morning (Australian eastern time).

I now think that I am programming the submitting and removing of the images in the wrong way. Because I am getting no where with want have been doing. By the way I need the user to be able to delete an image at anytime. Even after the attach photos window has been closed.

In the next post I am going to post the whole code. Maybe someone that is better at programming than I am, can help me.

Thank you chronister for helping me. You have been wonderful.
Link to comment
Share on other sites

[code=php:0]
require "config.php";
$findphoto = $_FILES['findphoto'];
$photo = mysql_real_escape_string($_POST['photo']);
$id = mysql_real_escape_string($_GET['id']);
$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'])) {
    $select = mysql_query("SELECT parent_id, photos FROM clearingsales_photos WHERE photos = '$photo' LIMIT 1") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
echo $parent_id;
    //$delete = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id' LIMIT 1";
//$query=mysql_query($delete) or die(print "Could not delete the entry because: " . mysql_error() . ". The query was $delete.");
        //print "<strong>Photo has been deleted</strong>.<br />";
    } //else {
//echo 'Form variable could not be found';
//}
echo $strError;
?>
<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.";
}
[/code]
Link to comment
Share on other sites

hello cturner try this if you still need a solution

("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
$query = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";
mysql_query($query ))
        if ($delete) {
        print "<strong>Photo has been deleted</strong>.<br />";
    } else {
        print "Could not delete the entry because: " . mysql_error() . ". The query was $query .";
    }
}
Link to comment
Share on other sites

I am getting this error now: Parse error: syntax error, unexpected ';' in /home/blu6592/public_html/hyc/admin/attachphoto2.php on line 60.

Here is the updated code:
[code=php:0]
if (isset($_POST['removebtn'])) {
("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select);
    $parent_id = $r['parent_id'];
$query = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";
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 ."; // line 60 is here

    }
}
[/code]
Link to comment
Share on other sites

Sorry I have been at my computer too long. It was the wrong page that I was looking for that error.

I am now getting this error: [i]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/blu6592/public_html/hyc/admin/attachphoto.php on line 53[/i] when I try to remove an image. I have commented where line 53 is.

Here is the code again:
[code=php:0]
if (isset($_POST['removebtn'])) {
("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'") or die ("Could not query because:" .mysql_error());
    $r = mysql_fetch_assoc($select); // line 53 is here
    $parent_id = $r['parent_id'];
$query = "DELETE FROM `clearingsales_photos` WHERE `parent_id` = '$parent_id'";
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;
    }
}
[/code]
Link to comment
Share on other sites

cturner,

I tell this everyone
print your query to your screen before you try to execute it.
if you have an error
run the query directly on the database and look what the error is you get.

We don't know your database layout. so it is hard to guess for us what may be wrong.

the error you get is typical for a sql syntax error.

anatak
Link to comment
Share on other sites

I am now getting Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/blu6592/public_html/hyc/admin/attachphoto.php on line 53
DELETE FROM clearingsales_photos WHERE parent_id =.

I have echoed out the query as you said and the above is what I got.
Link to comment
Share on other sites

DELETE FROM clearingsales_photos WHERE parent_id =.
well obviously you do not get the right id for parent_id
you try to run a query without the where clause completed
$select = mysql_query("SELECT parent_id FROM clearingsales_photos WHERE photos = '$photo'")
are you sure that that is working ?
echo this query too
and do a
print_r($select); to see if you have really something in there
Link to comment
Share on other sites

did you echo the sql to your screen ?
I am pretty sure it is a problem with your sql.
I don't see anything wrong in your php.

if you really want to learn to program i suggest you take a look at some tutorials.
the tutorials on the phpfreaks website are a good starting point.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.