emediastudios Posted October 13, 2007 Share Posted October 13, 2007 I use the dreamweaver generated code to delete a record, how would i add some code to it to delete the files (images) stored in my../images folder, that are linked to the record being deleted, there field names are photo1, photo2, ect..... and there image names are stored in the database Here is the dreamweaver code. <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $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; } } if ((isset($_GET['name'])) && ($_GET['name'] != "")) { $deleteSQL = sprintf("DELETE FROM employees WHERE name=%s", GetSQLValueString($_GET['name'], "text")); mysql_select_db($database_gcproperty, $gcproperty); $Result1 = mysql_query($deleteSQL, $gcproperty) or die(mysql_error()); $deleteGoTo = "property_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } if ((isset($_POST['name'])) && ($_POST['name'] != "")) { $deleteSQL = sprintf("DELETE FROM employees WHERE name=%s", GetSQLValueString($_POST['name'], "text")); mysql_select_db($database_gcproperty, $gcproperty); $Result1 = mysql_query($deleteSQL, $gcproperty) or die(mysql_error()); $deleteGoTo = "property_deleted.php"; if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $deleteGoTo)); } mysql_select_db($database_gcproperty, $gcproperty); $query_Recordset1 = "SELECT * FROM employees"; $Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73070-delete-record/ Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 I want to add a code similar to this i guess <?php $file_delete = "../images/<?php echo $row_Recordset1['photo']; ?>"; /////////-------------- dont think this is right-------------///////// if (unlink($file_delete)) { echo "The file was deleted successfully.", "\n"; } else { echo "The specified file could not be deleted. Please try again.", "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368488 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 If i add a recordset and this code <?php $file_delete = "../images/<?php echo $row_Recordset1['photo']; ?>"; if (unlink($file_delete)) { echo "The file was deleted successfully.", "\n"; } else { echo "The specified file could not be deleted. Please try again.", "\n"; } ?> I get this error Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\deletfiles.php on line 42 Quote Link to comment https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368490 Share on other sites More sharing options...
matto Posted October 13, 2007 Share Posted October 13, 2007 The first thing I would recommend would be to lose Dreamweaver if you ever want to fully understand PHP. The answer to your question: <?php $sql = "select image_name from database_table"; $result = @mysql_query($sql) or die("<tt>unable to execute $sql</tt>"); //path where images are stored $image_dir = "/images" while($r = @mysql_fetch_object($result)) { $old_file = $image_dir . "/" . $r->image_name; if(is_file($old_file)) { @unlink($old_file); } } ?> Note: The user that PHP runs as will need the correct permissions to remove files from the desired location. ) Quote Link to comment https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368493 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 Thanks, still a bigginer so i dont know where to start with your code. I have this code and when i execute the code it tries t delete a file called "testfile.txt, doesnt ork though as that file doesnt exsist. How would i change this so that it echos the $row_Recordset1['photo'] instead of testfile.txt? if (isset($_SERVER['QUERY_STRING'])) { $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?"; $deleteGoTo .= $_SERVER['QUERY_STRING']; $myFile = "testFile.txt"; unlink($myFile); Quote Link to comment https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368496 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.