Jump to content

Somebody please help me sort out this unlink thing!


Stevis2002

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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\"

Link to comment
Share on other sites

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

Link to comment
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.