emediastudios Posted October 13, 2007 Share Posted October 13, 2007 I have a code that uploads images along with some info into a directory and database, this all works fine. I can delete the record but the images relevent to that record continue to exsist in my images directory. How would i set up a php code to delete a file in ../images when the record of its name is stored in a column in my database. Here is my delete record code. The image records in my database are in a columns named photo1, photo2, etc.. <?php require_once('../Connections/gcproperty.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "restricted.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?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($_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); ?> <!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>Administration Panel</title> <style type="text/css"> <!-- .style1 {color: #000000} body { background-color: #000000; } .style2 { color: #FFFFFF; font-size: 10px; } --> </style> <link href="../css/admin.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- .style3 {font-size: 10px} .style4 {font-size: 16px} .style5 { color: #FFFFFF; font-size: 10px; } .style5 {color: #FFFFFF} --> </style> <link href="../css/main.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- .style7 {font-size: 18px} --> </style> </head> <body> <div id="container"><table width="780" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="8"><img src="../images/admin-header.jpg" alt="GC Propery" width="780" height="223" /></td> </tr> <tr> <td width="132" height="24" background="../images/navbar.jpg" class="name"><div align="center" class="admin"><a href="edit_privacy.php" target="_self">Terms / Privacy</a></div></td> <td width="91" background="../images/navbar.jpg" class="name"><div align="center" class="admin"> <a href="investour.php" target="_self">Investour</a></div></td> <td width="96" background="../images/navbar.jpg" class="name"><div align="center" class="admin"> <a href="property_add.php" target="_self">Property</a></div></td> <td width="99" background="../images/navbar.jpg" class="name"><div align="center" class="admin"><a href="news.php" target="_self">News</a></div></td> <td width="81" background="../images/navbar.jpg" class="name"><div align="center" class="admin"><a href="email.php" target="_self">email</a></div></td> <td width="108" background="../images/navbar.jpg" class="name"><div align="center" class="admin"> <a href="links.php" target="_self">Links</a></div></td> <td width="96" background="../images/navbar.jpg" class="name"><div align="center" class="admin"><a href="main.php" target="_self">Main Content</a></div></td> <td width="77" background="../images/navbar.jpg" class="name"><div align="center" class="admin"><a href="logout.php" target="_self" class="admin">Logout</a></div></td> </tr> <tr> <td height="2" colspan="8" background="../images/bar.jpg" class="admin"> </td> </tr> <tr> <td colspan="8" class="admin"><div align="left"> <blockquote> <p class="style2"><br /> <u><span class="style4">Edit GCProperty.</span></u><br /> <br /> <span class="style4"><a href="property_add.php" target="_self">Click here to Add Property <br /> </a></span></p> <?php if ($totalRows_Recordset1 == 0) { // Show if recordset empty ?> <table width="600" border="0" align="center"> <tr> <td><div align="center"><span class="name">There are no properties to delete</span></div></td> </tr> </table> <?php } // Show if recordset empty ?> <?php do { ?> <?php if ($totalRows_Recordset1 > 0) { // Show if recordset not empty ?> <form id="form1" name="form1" method="post" action=""> <table width="507" border="0" align="center" bgcolor="#333333"> <tr> <td colspan="3" bgcolor="#999999"><span class="style7">Delete Property</span></td> </tr> <tr> <td colspan="3">Delete a property from the GCPRoperty Database <hr /></td> </tr> <tr> <td width="251"><div align="right">Name:</div></td> <td width="26"> </td> <td width="216"><input name="name" type="text" id="name" value="<?php echo $row_Recordset1['name']; ?>" /></td> </tr> <tr> <td><div align="right">Suburb:</div></td> <td> </td> <td><input name = "suburb" type="text" id="suburb" value="<?php echo $row_Recordset1['suburb']; ?>" /></td> </tr> <tr> <td><div align="right">Price: </div></td> <td> </td> <td><input name = "price" type="text" id="price" value="<?php echo $row_Recordset1['price']; ?>" /></td> </tr> <tr> <td><div align="right">Photo:</div></td> <td> </td> <td><img src="../images/<?php echo $row_Recordset1['photo']; ?>" alt="image" width="150" /><br /> <label> <input name="photo" type="text" id="photo" value="<?php echo $row_Recordset1['photo']; ?>" /> </label></td> </tr> <tr> <td><div align="right">Photo2:</div></td> <td> </td> <td><img src="../images/<?php echo $row_Recordset1['photo2']; ?>" alt="image" width="150" /><br /> <label> <input name="photo2" type="text" id="photo2" value="<?php echo $row_Recordset1['photo2']; ?>" /> </label></td> </tr> <tr> <td><div align="right">Photo3:</div></td> <td> </td> <td><img src="../images/<?php echo $row_Recordset1['photo3']; ?>" alt="image" width="150" /><br /> <label> <input name="photo3" type="text" id="photo3" value="<?php echo $row_Recordset1['photo3']; ?>" /> </label></td> </tr> <tr> <td><div align="right">Text</div></td> <td> </td> <td><label> <textarea name="content" cols="40" rows="10" id="content"><?php echo $row_Recordset1['content']; ?></textarea> </label></td> </tr> <tr> <td> </td> <td> </td> <td><input name="Submit" type="submit" value="Delete" /></td> </tr> </table> <br /> </form> <?php } // Show if recordset not empty ?> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> <p align="center" class="style2"> </p> </blockquote> </div></td> </tr> <tr> <td colspan="8" class="admin"><div align="center"><br /> </div></td> </tr> <tr> <td colspan="8" class="admin"><br /> <br /></td> </tr> <tr> <td colspan="5"> </td> <td> </td> <td> </td> </tr> </table> <div align="center"><br /> <br /> </div> </body> </html> <?php mysql_free_result($Recordset1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73047-delete-record-and-files/ 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.