Stevis2002 Posted November 19, 2010 Share Posted November 19, 2010 Hi all, Please can somebody help me sort my script out? I want it to delete the sql contents in the chosen table, and delete the file stored on server at the same time. URL for server is stored in the database. Everything works except for when it comes to deleting the file off the server. I just can't work it out/get my head around it. Many Thanks in advance if you can help, Steve <?php require_once('Connections/localhost.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $query_Recordset1 = "SELECT Images FROM testimonials WHERE Testimonial_Id=%s"; $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); // unlink($img_dir.$image_name); $deleteSQL = sprintf("DELETE FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($deleteSQL, $localhost) or die(mysql_error()); $deleteGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $maxRows_Recordset1 = 10; $pageNum_Recordset1 = 0; if (isset($_GET['pageNum_Recordset1'])) { $pageNum_Recordset1 = $_GET['pageNum_Recordset1']; } $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1; mysql_select_db($database_localhost, $localhost); $query_Recordset1 = "SELECT * FROM testimonials ORDER BY SortOrder ASC"; $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1); $Recordset1 = mysql_query($query_limit_Recordset1, $localhost) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); if (isset($_GET['totalRows_Recordset1'])) { $totalRows_Recordset1 = $_GET['totalRows_Recordset1']; } else { $all_Recordset1 = mysql_query($query_Recordset1); $totalRows_Recordset1 = mysql_num_rows($all_Recordset1); } $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sub-Lime Renovations Admin Area - View Testimonials</title> </head> <body> <div align="center"> <h1><strong>Sub-Lime Renovations Administration Area</strong></h1> </div> <p align="center"><a href="index.php">Admin Home</a> | <a href="add_testimonials.php">Add Testimonials</a></p> <p> </p> <p> </p> <table border="1" align="center" cellpadding="1" cellspacing="1"> <tr> <td>Customer Name</td> <td>Town</td> <td>Testimonial</td> <td>Sort Order</td> <td>Images</td> </tr> <?php do { ?> <tr> <td><?php echo $row_Recordset1['CustomerName']; ?></td> <td><?php echo $row_Recordset1['Town']; ?></td> <td><?php echo $row_Recordset1['Testimonial']; ?></td> <td><?php echo $row_Recordset1['SortOrder']; ?></td> <td><img width ="100" height="100" src="/AdministrationAreaSublime/<?php echo $row_Recordset1['Images']; ?>" alt="" /></td> <td><a href="edit_testimonials.php?Testimonial_Id=<?php echo $row_Recordset1['Testimonial_Id']; ?>">Edit</a></td> <td><input type="button" name="del" id="del" value="Delete" onClick="document.location.href='testimonials.php?del=<?php echo $row_Recordset1['Testimonial_Id']?>'" /></td></tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> </table> <p> </p> </body> </html> <?php mysql_free_result($Recordset1); ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted November 19, 2010 Share Posted November 19, 2010 What does $image_name print out? Use this code <?php require_once('Connections/localhost.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $query_Recordset1 = "SELECT Images FROM testimonials WHERE Testimonial_Id=%s"; $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); // unlink($img_dir.$image_name); $deleteSQL = sprintf("DELETE FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($deleteSQL, $localhost) or die(mysql_error()); $deleteGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $maxRows_Recordset1 = 10; $pageNum_Recordset1 = 0; if (isset($_GET['pageNum_Recordset1'])) { $pageNum_Recordset1 = $_GET['pageNum_Recordset1']; } $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1; mysql_select_db($database_localhost, $localhost); $query_Recordset1 = "SELECT * FROM testimonials ORDER BY SortOrder ASC"; $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1); $Recordset1 = mysql_query($query_limit_Recordset1, $localhost) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); if (isset($_GET['totalRows_Recordset1'])) { $totalRows_Recordset1 = $_GET['totalRows_Recordset1']; } else { $all_Recordset1 = mysql_query($query_Recordset1); $totalRows_Recordset1 = mysql_num_rows($all_Recordset1); } $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sub-Lime Renovations Admin Area - View Testimonials</title> </head> <body> <div align="center"> <h1><strong>Sub-Lime Renovations Administration Area</strong></h1> </div> <p align="center"><a href="index.php">Admin Home</a> | <a href="add_testimonials.php">Add Testimonials</a></p> <p> </p> <p> </p> <table border="1" align="center" cellpadding="1" cellspacing="1"> <tr> <td>Customer Name</td> <td>Town</td> <td>Testimonial</td> <td>Sort Order</td> <td>Images</td> </tr> <?php do { ?> <tr> <td><?php echo $row_Recordset1['CustomerName']; ?></td> <td><?php echo $row_Recordset1['Town']; ?></td> <td><?php echo $row_Recordset1['Testimonial']; ?></td> <td><?php echo $row_Recordset1['SortOrder']; ?></td> <td><img width ="100" height="100" src="/AdministrationAreaSublime/<?php echo $row_Recordset1['Images']; ?>" alt="" /></td> <td><a href="edit_testimonials.php?Testimonial_Id=<?php echo $row_Recordset1['Testimonial_Id']; ?>">Edit</a></td> <td><input type="button" name="del" id="del" value="Delete" onClick="document.location.href='testimonials.php?del=<?php echo $row_Recordset1['Testimonial_Id']?>'" /></td></tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> </table> <p> </p> </body> </html> <?php mysql_free_result($Recordset1); ?> Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Returns a blank page only. The row Images in the database stores /uploaded_images/xxxx.jpg. It gets from the image name from the image the user uploads Quote Link to comment Share on other sites More sharing options...
papaface Posted November 19, 2010 Share Posted November 19, 2010 Just realised you're not actually running a query "SELECT Images FROM testimonials WHERE Testimonial_Id=%s"; should be mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id=%s"); and you need to move your database connection above this code so that there is a connection open to run the query Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks for all the help, but still blank page mysql_select_db($database_localhost, $localhost); mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id=%s"); $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks for all the help, but still blank page mysql_select_db($database_localhost, $localhost); mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id=%s"); $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); By the way, the db connection is opened at the top of the page Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Can anybody help me out some more with his please? Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 You're just executing the select statement into thin air - you're not capturing the rows that are being selected. $result = mysql_query(...); while($row = mysql_fetch_array($result)) { --- delete $row['Images'] --- } Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 You're just executing the select statement into thin air - you're not capturing the rows that are being selected. $result = mysql_query(...); while($row = mysql_fetch_array($result)) { --- delete $row['Images'] --- } Thanks for that but the row with the images contact is the row which holds the url information. The file is on the server and that is wjhat i cannot do Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 In your code above: if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); $row is never defined, so $row['Images'] is empty. That's what I was pointing out, you need to define $row from your query result. Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks very much for helping me with this Jon. My statement now looks like this, mysql_select_db($database_localhost, $localhost); mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id=%s"); $result = mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id=%s"); while($row = mysql_fetch_array($result)) { $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); // unlink($img_dir.$image_name); } but it is giving me this error..... Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\AdministrationAreaSublime\testimonials.php on line 36 Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 You've got an error in your mysql. Add "echo mysql_error()" immediately after your query. You don't need mysql_query twice - just keep the one with $result = ... And your error is probably in the %s - if that's the string you're searching for, you need to enclose it in single quotes. If you're intending to do something with sprintf to populate that as a string, you need to include that function to get your query to be correct. Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Fixed the error, it is as you said, but now nothing deletes.....file on server doesn't delete, and neither does the database info. mysql_select_db($database_localhost, $localhost); $result = mysql_query("SELECT Images FROM testimonials WHERE Testimonial_Id='%s'"); echo mysql_error(); while($row = mysql_fetch_array($result)) { $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); // unlink($img_dir.$image_name); } $deleteSQL = sprintf("DELETE FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($deleteSQL, $localhost) or die(mysql_error()); $deleteGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 Your script is ending before you do any deleting - which is probably good at this point, as you have this code in: echo $image_name; die(); Is an image name being echoed? Verify that it's the file you want to delete, then remove the line and the rest of your script should run. Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 No image name is being echoed. Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 What is the result of the query if you execute it in PHPMyAdmin? SELECT Images FROM testimonials WHERE Testimonial_Id='%s' I'm going to guess that you're needing to set Testimonial_Id to a numeric value instead of '%s' Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 What is the result of the query if you execute it in PHPMyAdmin? SELECT Images FROM testimonials WHERE Testimonial_Id='%s' I'm going to guess that you're needing to set Testimonial_Id to a numeric value instead of '%s' I was just going along the lines of, that if the string contains the info (id) for which set of database details to delete, then it will also contain the correct info (id) to choose which file from the server to delete? Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 That query will find any rows in the Images table that have the Testimonial_Id column ending in the letter 's'. So if your Testimonial_Id is numeric, it will find nothing. If your testimonial_id has values like "Blue","Cars","Dogs", it would find "Cars" and "Dogs." If you're intending to replace %s with a variable, you need to use sprintf - otherwise, your query contains a literal "%s" Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Still nothing mate. I'm getting a headache now lol mysql_select_db($database_localhost, $localhost); $deleteSQL2 = sprintf("SELECT Images FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); $result = mysql_query($deleteSQL2 , $localhost) or die(mysql_error()); //echo mysql_error(); while($row = mysql_fetch_array($result)) { $img_dir = 'uploaded_images/'; $image_name = $row_RecordSet1['Images']; echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); } $deleteSQL = sprintf("DELETE FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($deleteSQL, $localhost) or die(mysql_error()); $deleteGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 Change this: $deleteSQL2 = sprintf("SELECT Images FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); $result = mysql_query($deleteSQL2 , $localhost) or die(mysql_error()); //echo mysql_error(); to this: $deleteSQL2 = sprintf("SELECT Images FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); echo "<P>".$deleteSQL2; $result = mysql_query($deleteSQL2) or die(mysql_error()); //echo mysql_error(); First, you don't need the $localhost argument to mysql_query (that alone is probably breaking it). If it still doesn't work, look at the query that is printed via the echo command and make sure it's what you're expecting. Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Getting somewhere now...Deletes the info again but it states that there is no such file, when i know there is... Warning: unlink(/AdministrationAreaSublime/uploaded_images/compfactorslogo.gif) [function.unlink]: No such file or directory in C:\xampp\htdocs\AdministrationAreaSublime\testimonials.php on line 44 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\AdministrationAreaSublime\testimonials.php:36) in C:\xampp\htdocs\AdministrationAreaSublime\testimonials.php on line 57 mysql_select_db($database_localhost, $localhost); $deleteSQL2 = sprintf("SELECT Images FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); echo "<P>".$deleteSQL2; $result = mysql_query($deleteSQL2) or die(mysql_error()); //echo mysql_error(); while($row = mysql_fetch_array($result)) { $img_dir = '/AdministrationAreaSublime/'; $image_name = $row_RecordSet1['Images']; //echo $image_name; die(); if ((isset($_GET['del'])) && ($_GET['del'] != "")) { unlink($img_dir . $row['Images']); } $deleteSQL = sprintf("DELETE FROM testimonials WHERE Testimonial_Id=%s", GetSQLValueString($_GET['del'], "int")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($deleteSQL, $localhost) or die(mysql_error()); $deleteGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 Ah, you're on a Windows server. I'm sorry for your pain. I'm going to guess that there is an issue with your $img_dir variable - remember unlink is using the system path, not the html path. So you probably have it set to "/AdministrationAreaSublime/uploaded_images/" which is what you need for using it to populate an <img> tag, but you're going to have to have a different value that you use for the unlink command that is something like "C:\xampp\htdocs\AdministrationAreaSublime\uploaded_images\" Quote Link to comment Share on other sites More sharing options...
Stevis2002 Posted November 19, 2010 Author Share Posted November 19, 2010 Ah, you're on a Windows server. I'm sorry for your pain. I'm going to guess that there is an issue with your $img_dir variable - remember unlink is using the system path, not the html path. So you probably have it set to "/AdministrationAreaSublime/uploaded_images/" which is what you need for using it to populate an <img> tag, but you're going to have to have a different value that you use for the unlink command that is something like "C:\xampp\htdocs\AdministrationAreaSublime\uploaded_images\" You sir are a star!!!!!!!!!!!!!!!!!!!!!! Thank you ever so much, just had to change the path like you suggested, and swap the \ to / Thank you ever so much for all your help, time and patience, Steve 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.