Hi All

I am trying to create a page to delete records from a database, but i cannot seem to get the right code in

hereunder please find my code

<?php require_once('Connections/conn.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['productID'])) && ($_GET['productID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM Products WHERE productCode=%s",
                       GetSQLValueString($_GET['productID'], "text"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($deleteSQL, $conn) or die(mysql_error());
}

mysql_select_db($database_conn, $conn);
$query_delete = "SELECT productID, productCode, productName, productDesc, productPrice FROM tblproducts ORDER BY productID ASC";
$delete = mysql_query($query_delete, $conn) or die(mysql_error());
$row_delete = mysql_fetch_assoc($delete);
$totalRows_delete = mysql_num_rows($delete);
?><!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>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
Product ID <?php echo $row_delete['productID']; ?> Product Code<?php echo $row_delete['productCode']; ?>
  <label></label>
Product Name<?php echo $row_delete['productName']; ?>
  <input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_delete['productID']; ?>" />
  <input type="submit" name="delete" id="delete" value="Submit" />
</form>
 product 
</body>
</html>
<?php
mysql_free_result($delete);
?>

Recommended Answers

All 8 Replies

Hi All

I am trying to create a page to delete records from a database, but i cannot seem to get the right code in

hereunder please find my code

<?php require_once('Connections/conn.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['productID'])) && ($_GET['productID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM Products WHERE productCode=%s",
                       GetSQLValueString($_GET['productID'], "text"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($deleteSQL, $conn) or die(mysql_error());
}

mysql_select_db($database_conn, $conn);
$query_delete = "SELECT productID, productCode, productName, productDesc, productPrice FROM tblproducts ORDER BY productID ASC";
$delete = mysql_query($query_delete, $conn) or die(mysql_error());
$row_delete = mysql_fetch_assoc($delete);
$totalRows_delete = mysql_num_rows($delete);
?><!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>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
Product ID <?php echo $row_delete['productID']; ?> Product Code<?php echo $row_delete['productCode']; ?>
  <label></label>
Product Name<?php echo $row_delete['productName']; ?>
  <input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_delete['productID']; ?>" />
  <input type="submit" name="delete" id="delete" value="Submit" />
</form>
 product 
</body>
</html>
<?php
mysql_free_result($delete);
?> 

end quote.

Do you get any errors?
What is expected result and what is the result you getting?

You have posted quite a bit already, so you should know how to use code tags. Do it.

Do you get any errors?
What is expected result and what is the result you getting?

i am trying to bring up all the records on screen, and then i decide which ones to delete.

TEsting

<?php require_once('../../Connections/conn.php'); ?><?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../../umr/login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

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

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE schedule SET `day`=%s, `date`=%s,`time`=%s, `vs`=%s, `results`=%s, `section`=%s, `pitch`=%s, `competition`=%s, `photos`=%s, `Year`=%s WHERE `index`=%s" ,
                       GetSQLValueString($_POST['day'], "text"),
                       GetSQLValueString($_POST['date'], "date"),
					   GetSQLValueString($_POST['time'], "date"),
                       GetSQLValueString($_POST['vs'], "text"),
                       GetSQLValueString($_POST['results'], "text"),
                       GetSQLValueString($_POST['section'], "text"),
                       GetSQLValueString($_POST['pitch'], "text"),
                       GetSQLValueString($_POST['competition'], "text"),
                       GetSQLValueString($_POST['photos'], "text"),
					   GetSQLValueString($_POST['Year'], "text"),
					   GetSQLValueString($_POST['index'], "int"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());
}
// WHERE date <= DATE_SUB(curdate(), INTERVAL 1 DAY)
$maxRows_edit = 25;
$pageNum_edit = 0;
if (isset($_GET['pageNum_edit'])) {
  $pageNum_edit = $_GET['pageNum_edit'];
}
$startRow_edit = $pageNum_edit * $maxRows_edit;

mysql_select_db($database_conn, $conn);
$query_edit = "SELECT schedule.index, schedule.`day`, schedule.`date`, schedule.`time`, schedule.`vs`, schedule.`results`, schedule.`section`, schedule.`pitch`, schedule.`competition`, schedule.`photos`, schedule.`Year` FROM schedule WHERE date < DATE_SUB(curdate(), INTERVAL 1 DAY)ORDER BY schedule.`date`";
$query_limit_edit = sprintf("%s LIMIT %d, %d", $query_edit, $startRow_edit, $maxRows_edit);
$edit = mysql_query($query_limit_edit, $conn) or die(mysql_error());
$row_edit = mysql_fetch_assoc($edit);

if (isset($_GET['totalRows_edit'])) {
  $totalRows_edit = $_GET['totalRows_edit'];
} else {
  $all_edit = mysql_query($query_edit);
  $totalRows_edit = mysql_num_rows($all_edit);
}
$totalPages_edit = ceil($totalRows_edit/$maxRows_edit)-1;

$queryString_edit = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_edit") == false && 
        stristr($param, "totalRows_edit") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_edit = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_edit = sprintf("&totalRows_edit=%d%s", $totalRows_edit, $queryString_edit);
?>
<!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=iso-8859-1" />
<title>Schedule edit</title>
</head>

<body>
<?php do { ?>
  <form action="<?php echo $row_delete['']; ?>" method="POST" name="form1">
    <table align="center">
      <tr valign="baseline">
        <td nowrap align="right">Index:</td>
        <td><input name="index" type="text" value="<?php echo $row_edit['index']; ?>" size="4" maxlength="4">
          Year
          <input name="Year" type="text" value="<?php echo $row_edit['Year']; ?>" size="4" maxlength="4">
          Day
          <input type="text" name="day2" value="<?php echo $row_edit['day']; ?>" size="4" />
          Date
          <input type="text" name="date" value="<?php echo $row_edit['date']; ?>" size="10" />
          Time
  <input type="text" name="time" value="<?php echo $row_edit['time']; ?>" size="8" />
          Vs
  <input type="text" name="vs" value="<?php echo $row_edit['vs']; ?>" size="20" />
          Results
  <input type="text" name="results" value="<?php echo $row_edit['results']; ?>" size="5" />
          Section
  <input name="section" type="text" value="<?php echo $row_edit['section']; ?>" size="10" />
          Pitch
  <input type="text" name="pitch" value="<?php echo $row_edit['pitch']; ?>" size="15" />
          Competition
  <input type="text" name="competition" value="<?php echo $row_edit['competition']; ?>" size="12" />
          Photos
  <input type="text" name="photos" value="<?php echo $row_edit['photos']; ?>" size="20" />
  <input type="submit" value="Update record" /></td>
      </tr>
    </table>
    <input type="hidden" name="MM_delete" value="form1">
    <input type="hidden" name="day" value="<?php echo $row_edit['day']; ?>">
      </form>
  <?php } while ($row_edit = mysql_fetch_assoc($edit)); ?><p>
<table border="0" width="50%" align="center">
  <tr>
    <td width="23%" height="34" align="center"><?php if ($pageNum_edit > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_edit=%d%s", $currentPage, 0, $queryString_edit); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
  <td width="31%" align="center"><?php if ($pageNum_edit > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_edit=%d%s", $currentPage, max(0, $pageNum_edit - 1), $queryString_edit); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_edit < $totalPages_edit) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_edit=%d%s", $currentPage, min($totalPages_edit, $pageNum_edit + 1), $queryString_edit); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_edit < $totalPages_edit) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_edit=%d%s", $currentPage, $totalPages_edit, $queryString_edit); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
</p>
</body>
</html>
<?php
mysql_free_result($edit);

mysql_free_result($delete);
?>

Okay, so what does your code actually output?

Okay, so what does your code actually output?

What i am trying to do is displaying a list of records, and then choose manually which ones to delete, by having a delete button next to every record.

Yes, I understand what you want to do. What is the problem with your code? What is it doing wrong?

Yes, I understand what you want to do. What is the problem with your code? What is it doing wrong?

i get no errors, but the record does not get deleted either.
when i press the delete button

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.