Forums

PHP

This topic is locked

duplicate key

Posted 19 Oct 2006 07:19:07
1
has voted
19 Oct 2006 07:19:07 ede edd posted:
Hi ,

i've created a table"user" that has :

uid|username

and set the uid as primary key and the username as unique so if u entered a name that has already entered it will return mysql warning "Duplicate entry 'yaddayaddayadda' for key 2"

how do i change the warning so that it open a page saying "u have entered a duplicate value "?im using dmx n mysql


hope u could help


this is code used to input values to tables
[php]

this the code i use to input values to table :
[php]<?php require_once('Connections/conn.php'); ?>
<?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_insert"])) && ($_POST["MM_insert"] == "form1") {
$insertSQL = sprintf("INSERT INTO diners (Name, subject) VALUES (%s, %s)",
GetSQLValueString($_POST['Name'], "text",
GetSQLValueString($_POST['subject'], "text");

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

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

mysql_select_db($database_conn, $conn);
$query_rs_user = "SELECT * FROM diners";
$rs_user = mysql_query($query_rs_user, $conn) or die(mysql_error());
$row_rs_user = mysql_fetch_assoc($rs_user);
$totalRows_rs_user = mysql_num_rows($rs_user);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" name="form1" method="POST">
<p>
Name
<input name="Name" type="text" id="Name">
</p>
<p> subject
<input name="subject" type="text" id="subject">
<input type="submit" name="Submit" value="Submit">
</p>

<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rs_user);
?> [/php]

hope u could help

Reply to this topic