View Full Version : Duplicate entry error

10-22-2004, 02:08 PM
Hi ,

i know this is also related to mysql but i think the problem is actually with my PHP code.

anyway here s the problem.

i have a table containing text fields etc and that contains the data stored for the member in the DB
when the submit button is hit the db should get updated but all im getting is duplicate entry for " key 2

here is the query

$currentPage = $_SERVER["PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
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 members SET memid=%, password=%s, realname=%s, age=%s, location=%s, sex=%s, country=%s, starsign=%s, sexuality=%s, profession=%s, marital_status=%s, hair=%s, eyes=%s, height=%s, body_type=%s, race=%s, lookingfor=%s, seeking=%s, likes=%s, dislikes=%s, emailaddress=%s, pwdreminder=%s, my2cents=%s WHERE username='$_SESSION[MM_Username]'",
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['realname'], "text"),
GetSQLValueString($_POST['age'], "int"),
GetSQLValueString($_POST['location'], "text"),
GetSQLValueString($_POST['sex'], "text"),
GetSQLValueString($_POST['country'], "text"),
GetSQLValueString($_POST['starsign'], "text"),
GetSQLValueString($_POST['sexuality'], "text"),
GetSQLValueString($_POST['profession'], "text"),
GetSQLValueString($_POST['marital_status'], "text"),
GetSQLValueString($_POST['hair'], "text"),
GetSQLValueString($_POST['eyes'], "text"),
GetSQLValueString($_POST['height'], "text"),
GetSQLValueString($_POST['body_type'], "text"),
GetSQLValueString($_POST['race'], "text"),
GetSQLValueString($_POST['lookingfor'], "text"),
GetSQLValueString($_POST['seeking'], "text"),
GetSQLValueString($_POST['likes'], "text"),
GetSQLValueString($_POST['dislikes'], "text"),
GetSQLValueString($_POST['emailaddress'], "text"),
GetSQLValueString($_POST['pwdreminder'], "text"),
GetSQLValueString($_POST['my2cents'], "text"),
GetSQLValueString($_POST['memid'], "int"));

mysql_select_db($database_OnlineFlirting, $OnlineFlirting);
$Result1 = mysql_query($updateSQL, $OnlineFlirting) or die(mysql_error());

$updateGoTo = "myaccount.php?messages=Profile Successfully Updated";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
header(sprintf("Location: %s", $updateGoTo));

my HTML form is fine so the probelm lies in the above code
i need this solved asap as i have already passed my deadline
any help would be greatly appreciated


10-22-2004, 02:23 PM
it's a dataaseproblem. Check the table you are updating. There is an index with unique values and your updatestatement tries to update a record and give it an already existing value for that index.

It could help to echo out $updateSQL and then look for the duplicate value.

10-22-2004, 02:44 PM
Raf your a life saver...
it made me realise that the emailaddress was a hiddenfield and that it was being passed empty ( theres already 1 Null filed in the db ) hence the error

thanks so much !!!

10-22-2004, 03:53 PM
you're welcome :thumbsup: