We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Php-Mysql delete from database and server

sinanosan
sinanosan asked
on
Medium Priority
408 Views
Last Modified: 2013-12-12
I can delete a record (image file name) with attached code from my database, but in the same time I want to delete this file from my server too , is it possible? how can I do this? thanks
<?php require_once('../../Connections/php.php'); ?>
<?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['photo_id'])) && ($_GET['photo_id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM tbl_gallery WHERE photo_id=%s",
                       GetSQLValueString($_GET['photo_id'], "int"));
 
  mysql_select_db($database_php, $php);
  $Result1 = mysql_query($deleteSQL, $php) or die(mysql_error());
 
  $deleteGoTo = "photos.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
?>

Open in new window

Comment
Watch Question

Author

Commented:
File is stored in folder called photos
CERTIFIED EXPERT
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Thanks fsze88 links helped, and if anybody needs it, here is the complete code to delete from database and server in same time, $_GET['photo'] gets the folder name and filename from previous form
<?php require_once('../../Connections/php.php'); ?>
<?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['photo_id'])) && ($_GET['photo_id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM tbl_gallery WHERE photo_id=%s",
                       GetSQLValueString($_GET['photo_id'], "int"));
 
  mysql_select_db($database_php, $php);
  $Result1 = mysql_query($deleteSQL, $php) or die(mysql_error());
 
  $deleteGoTo = "photos.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
?>
<?
$delete = unlink($_GET['photo']);
 
if($delete)
{
echo "Succesfully Deleted";
}
else
{
echo "Could not Delete";
}
?>

Open in new window

CERTIFIED EXPERT

Commented:
sinanosan,
you are using unlink function, right?
I am able to guide you how to delete the file from server..
Why you close this question and no points assign??

Author

Commented:
I thought I assigned the points
CERTIFIED EXPERT

Commented:
would you mind to assign points for me??
Thanks a lot
;)

Author

Commented:
Thanks, thats what I meant
CERTIFIED EXPERT

Commented:
Okey, up to sinanosan or EE Moderator
I would like to happiness to everyone
;)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.