Jump to content

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/73070-delete-record/
Share on other sites

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";
   }
?>


Link to comment
https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368488
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368490
Share on other sites

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.

 

:o)

Link to comment
https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368493
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/73070-delete-record/#findComment-368496
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.