hana2410 Posted May 10, 2006 Share Posted May 10, 2006 Hi,I have created a form where people can update their information with the option of deleting their picture using a checkbox. If a person wishes to to delete their picture, all they need to the do is click on the checkbox and their picture will be removed when the form is updated. My problem is that I can not get my script to unlink and delete the image location from my database. The update script for the other form fields works fine. I'm not sure what I am doing wrong as I do not receive any error messages.Here's my unlink code:[code]$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if (isset($editFormAction)) { if ((isset($_POST['remove_pic'])) && ($_POST['remove_pic'] == "cbox")) { mysql_select_db($database_conn, $conn) or die(mysql_error()); $id = (isset($_POST['id'])) or die(mysql_error()); $sql = "Select file from table Where id = '" .$id. "' " or die(mysql_error()); $result2 = mysql_query($sql, $conn) or die(mysql_error()); $ddata2 = mysql_fetch_assoc($result2) or die(mysql_error()); $name = $ddata2['file'] or die(mysql_error()); if (is_file($name)) { unlink($name) or die(mysql_error()); } }}[/code]Here's my form code:[code]<?phpif ($row_info['file']!= "") {echo '<tr><td nowrap div align="right" class="style6">Current Picture</td> <td><img src="' .$row_info['file']. '"></td> </tr><tr><td nowrap div align="right" class="style6">Delete Current Picture?</td><td><input type="checkbox" name="remove_pic" value="cbox"></td><td class="style14">(To delete your picture, tick the checkbox) </td></tr>';} else {echo "";}?>[/code]I also have a hidden field in my form that has the account id in it.I appreciate any help in helping me to get my script to work.Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/9440-unlink-images-using-checkbox/ Share on other sites More sharing options...
.josh Posted May 10, 2006 Share Posted May 10, 2006 unlink deletes a file from the directory. it does not delete an entry from a row in your table. all you are doing is selecting the file name and deleting the file from the server. and btw you need to nix most of those "or die(mysql_error()) endings, especially this one here:$sql = "Select file from table Where id = '" .$id. "' " [!--coloro:red--][span style=\"color:red\"][!--/coloro--]or die(mysql_error());[!--colorc--][/span][!--/colorc--]it should be this:$sql = "Select file from table Where id = '" .$id. "' ";msyql_error() returns the error message from an actual query operation, so it doesn't even apply to most of those lines of code.this first part of your code looks pretty shady:[code]$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}[/code]i'm not entirely sure what you're trying to accomplish with it... but just so you know, the if (isset($editFormAction)) {directly after it will always be true because you have that$editFormAction = $_SERVER['PHP_SELF'];up there.as far as the rest of your code, i cleaned it up a bit:[code]if (isset($editFormAction)) { if ((isset($_POST['remove_pic'])) && ($_POST['remove_pic'] == "cbox")) { mysql_select_db($database_conn, $conn) or die(mysql_error()); if ($_POST['id']){ $id = $_POST['id']; } $sql = "Select file from table Where id = '" .$id. "'"; $result2 = mysql_query($sql, $conn) or die(mysql_error()); $ddata2 = mysql_fetch_assoc($result2) or die(mysql_error()); $name = $ddata2['file']; if (is_file($name)) { unlink($name); } }}[/code]as far as deleting the image url from your database, you need to do something like this:[!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]update table set file='NULL' where id='$id'[!--colorc--][/span][!--/colorc--] or some other such thing as 'NULL' . however you want to delete it. maybe alternately, put 'none' and have your script check for 'none' when it comes to displaying a picture. Quote Link to comment https://forums.phpfreaks.com/topic/9440-unlink-images-using-checkbox/#findComment-34818 Share on other sites More sharing options...
hana2410 Posted May 10, 2006 Author Share Posted May 10, 2006 Thank you Crayon Violent for letting me know that unlink only deletes the file out of the directory but not the database as well as showing me how to update my database to display a Null value. After some more testing, I've realised that the reason why my images are not being deleted from the directory is that the "remove_pic" and $accountid variables are not being passed onto the script which is above the form. The form action is $editFormAction. When I take away the isset function I get index and variable undefined messages. Does anyone know I can pass the values of the variables in the form to the script?Your help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/9440-unlink-images-using-checkbox/#findComment-34840 Share on other sites More sharing options...
.josh Posted May 11, 2006 Share Posted May 11, 2006 there are lots of different methods of printing your form onto the webpage. you can have a .html file that displays it and have it post to a seperate .php file to process, or you can have your .php file echo the form onto the page if one or more or all fields in the form were not selected/something entered into them.but basically it goes like this:[code]<form method = 'post' action ='urlofscript.php'> enter something in here <input type = 'text' name = 'blah'> <br> <input type = 'submit' value = 'send'></form>[/code]and then in your script, you can retrieve what you entered into the text field using this variable:$_POST['blah']i personally like setting another variable to it, because that's one less set of quotes i have to deal with throughout the rest of my script:$blah = $_POST['blah']; Quote Link to comment https://forums.phpfreaks.com/topic/9440-unlink-images-using-checkbox/#findComment-35206 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.