Jump to content

[SOLVED] Delete Record


emediastudios

Recommended Posts

I have records in my database that all have a id number that is unique ,

But if i have the same name on a record and i delete the record from a dreamweaver generated code it deletes all records of the same name. I want to filter my deletion by id so that if i had 10 properties with the same name it would only delete the selected record?

Does this make sense.

 

Heres the code i use in my file

<?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['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM employees WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));

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

$myFile = array();
for($i=1;$i<=9;$i++) {
$myFile[$i] = "../images/" . $_POST['photo' . $i];
     if (!empty($_POST['photo'.$i]))
          unlink($myFile[$i]);
}

  
}
  
  header("Location: $deleteGoTo");

}

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM employees ORDER BY id ASC";
$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
Share on other sites

I got it to work ;D

But this code looks wrong there are duplicate codes

 

<?php
if ((isset($_POST['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM employees WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));

  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['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM employees WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));

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

$myFile = array();
for($i=1;$i<=9;$i++) {
$myFile[$i] = "../images/" . $_POST['photo' . $i];
     if (!empty($_POST['photo'.$i]))
          unlink($myFile[$i]);
}

  
}
  
  header("Location: $deleteGoTo");

}

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM employees ORDER BY id ASC";
$Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

I deleted the duplicated code but i get errors.

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.