Jump to content

Archived

This topic is now archived and is closed to further replies.

eaglesphan

Edit record inserts instead

Recommended Posts

These pages used to work correctly but now they don't and I just need guidance on where to look to find the problem.

 

I have a page that gets all information from a directory table according to last names. This page works fine. All records fetched have a link to Edit or Delete. When clicking on Edit, the information for the record is passed to another form with all the fields filled in. This works fine too. The problem is that when I edit a piece of information and submit, the form ADDS the corrected record rather than updating the information so I get a whole new record - which is wrong.

 

What do I check to find out why the form is adding a new record rather than updating the current one?

 

Here is the code for my edit record set on the problem page:

 

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
 $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 Directory SET last_name=%s, first_name=%s, title=%s, rank=%s, job_title1=%s, job_title2=%s, dept_num=%s, location=%s, room=%s, phone=%s, fax=%s, email=%s, doctor=%s, master=%s, bachelor=%s, associate=%s, tier=%s WHERE id=%s",
                      GetSQLValueString($_POST['last_name'], "text"),
                      GetSQLValueString($_POST['first_name'], "text"),
                      GetSQLValueString($_POST['title'], "text"),
                      GetSQLValueString($_POST['rank'], "text"),
                      GetSQLValueString($_POST['job_title1'], "text"),
                      GetSQLValueString($_POST['job_title2'], "text"),
                      GetSQLValueString($_POST['dept_num'], "int"),
                      GetSQLValueString($_POST['location'], "text"),
                      GetSQLValueString($_POST['room'], "text"),
                      GetSQLValueString($_POST['phone'], "text"),
                      GetSQLValueString($_POST['fax'], "text"),
                      GetSQLValueString($_POST['email'], "text"),
                      GetSQLValueString($_POST['doctor'], "text"),
                      GetSQLValueString($_POST['master'], "text"),
                      GetSQLValueString($_POST['bachelor'], "text"),
                      GetSQLValueString($_POST['associate'], "text"),
                      GetSQLValueString($_POST['tier'], "int"),
                      GetSQLValueString($_POST['tier'], "int"));

 mysql_select_db($database_connDESU, $connDESU);
 $Result1 = mysql_query($updateSQL, $connDESU) or die(mysql_error());

 $updateGoTo = "directory_mod.php";
 if (isset($_SERVER['QUERY_STRING'])) {
   $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
   $updateGoTo .= $_SERVER['QUERY_STRING'];
 }
 header(sprintf("Location: %s", $updateGoTo));
}

$colname_rsDirectoryEdit = "1";
if (isset($_GET['id'])) {
 $colname_rsDirectoryEdit = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_connDESU, $connDESU);
$query_rsDirectoryEdit = sprintf("SELECT * FROM Directory WHERE id = %s", $colname_rsDirectoryEdit);
$rsDirectoryEdit = mysql_query($query_rsDirectoryEdit, $connDESU) or die(mysql_error());
$row_rsDirectoryEdit = mysql_fetch_assoc($rsDirectoryEdit);
$totalRows_rsDirectoryEdit = mysql_num_rows($rsDirectoryEdit);
?>

Share this post


Link to post
Share on other sites

×

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.