cturner Posted January 29, 2007 Share Posted January 29, 2007 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] Quote Link to comment Share on other sites More sharing options...
chronister Posted January 29, 2007 Share Posted January 29, 2007 [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? Quote Link to comment Share on other sites More sharing options...
cturner Posted January 29, 2007 Author Share Posted January 29, 2007 I have changed my code as chronister told me to and still nothing happens. I am getting no errors. Quote Link to comment Share on other sites More sharing options...
chronister Posted January 29, 2007 Share Posted January 29, 2007 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]<?phpif (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] Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 [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] Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 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 ."; }} Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 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] Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 is this line 60 ?mysql_query($query ));it should be mysql_query($query);try this.if you have errors please tell us what the line is that causes the error Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 I have done that anatak and am still getting that error. I have commented where line 60 is in the code above. Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 print "Could not delete the entry because: " . mysql_error() . ". The query was ".$query; Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 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] Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 cturner,I tell this everyone print your query to your screen before you try to execute it.if you have an errorrun 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 Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 Well here are the tables:[b]clearingsales[/b][u]id[/u] // the primary key[i]the rest isn't relevant[/i][b]clearingsales_photos[/b][u]photos_id[/u] // the primary keyparent_idphotos Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 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 53DELETE FROM clearingsales_photos WHERE parent_id =.I have echoed out the query as you said and the above is what I got. Quote Link to comment Share on other sites More sharing options...
chronister Posted January 30, 2007 Share Posted January 30, 2007 Your not getting the parent_id placed in the query properly.You have $_GET for id but you should have $_POST['id']You are passing this variable using a hidden form field so you should use post as that is the method you are using to submit the form. Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 The id actually should come from the url. Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 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 tooand do a print_r($select); to see if you have really something in there Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 What about this error that I get when I press the remove button: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blu6592/public_html/hyc/admin/attachphoto.php on line 54. Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 cturner you should really start to work more independant.we can not write everything for you.the error you get means that you have probably an sql syntax error as I explained before.check your SQL Quote Link to comment Share on other sites More sharing options...
cturner Posted January 30, 2007 Author Share Posted January 30, 2007 I am trying to learn how to program in PHP and MySQL.I don't want anyone to write the whole program for me. All I want is help when I am stuck. Like now. Quote Link to comment Share on other sites More sharing options...
anatak Posted January 30, 2007 Share Posted January 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.