Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Php-Mysql delete from database and server

Posted on 2009-02-09
9
Medium Priority
?
346 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

0
Comment
Question by:sinanosan
  • 4
  • 4
8 Comments
 

Author Comment

by:sinanosan
ID: 23596041
File is stored in folder called photos
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 800 total points
ID: 23596697
0
 

Accepted Solution

by:
sinanosan earned 0 total points
ID: 23599938
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:fsze88
ID: 23600341
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??
0
 

Author Comment

by:sinanosan
ID: 23600977
I thought I assigned the points
0
 
LVL 15

Expert Comment

by:fsze88
ID: 23601276
would you mind to assign points for me??
Thanks a lot
;)
0
 

Author Comment

by:sinanosan
ID: 23602495
Thanks, thats what I meant
0
 
LVL 15

Expert Comment

by:fsze88
ID: 23643783
Okey, up to sinanosan or EE Moderator
I would like to happiness to everyone
;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question