Forum Moderators: coopster
Could not insert record: Duplicate entry 'email@example.com' for key 1
How can i customize this so that it can be "this email is already in use." or something along those lines.
Should i write a function to do this?
Cheers
[edited by: eelixduppy at 4:23 pm (utc) on Dec. 27, 2007]
// connect to database
// do anything else you want
$insert = *sql_query('INSERT INTO information(email) VALUES 'me@example.com');
if (!$insert) {
echo 'Didnt work...Have a nice life ;)';
}
<?php
//functions to use
//email validator
function check_email_address($email) {
// check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{¦}~-][A-Za-z0-9!#$%&'*+/=?^_`{¦}~\.-]{0,63})¦(\"[^(\\¦\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])¦([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
//postcode validator
function IsPostcode($postcode) {
$postcode = strtoupper(str_replace(chr(32),'',$postcode));
if(ereg("^(GIR0AA)¦(TDCU1ZZ)¦((([A-PR-UWYZ][0-9][0-9]?)¦"
."(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)¦"
."(([A-PR-UWYZ][0-9][A-HJKSTUW])¦"
."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
."[0-9][ABD-HJLNP-UW-Z]{2})$", $postcode))
return $postcode;
else
return FALSE;
}
//connect to server and select database
$mysql = mysql_connect("localhost","user","pass");
mysql_select_db("db");
//if connection connection fails
if (!$mysql) {
echo "connection failure";
exit();
//insert into table
} else{
if(check_email_address($email) === TRUE){
if(IsPostcode($postcode)!= FALSE){
$fname = $_POST['firstname'];
$sname = $_POST['surname'];
$addr1 = $_POST['addr1'];
$addr2 = $_POST['addr2'];
$addr3 = $_POST['addr3'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$sql = "INSERT INTO collection (firstname,surname,addr1,addr2,addr3,town,county,postcode,email) VALUES('$fname','$sname','$addr1','$addr2','$addr3','$town','$county','$postcode','$email')";
$res = mysql_query($sql,$mysql);
}
}
//success or failed messages
if(check_email_address($email) === FALSE)
{
echo "Email is invalid";
}
if(IsPostcode($postcode) === FALSE)
{
echo "Postcode is invalid";
}
if ($res === TRUE) {
echo "A record has been inserted";
} else {
echo "Could not insert record:\n".mysql_error();
mysql_close($mysql);
}
}
?>
I want to have a message saying that the email is already in use/signed up.
As i have two functions in use , should i just write another or is there a simpler way of doing it?
Cheers
$sql = "INSERT INTO collection (firstname,surname,addr1,addr2,addr3,town,county,postcode,email) VALUES('$fname','$sname','$addr1','$addr2','$addr3','$town','$county','$postcode','$email')";
$res = mysql_query($sql,$mysql);
if(!$res)
{
echo"email exists <a href='asasa.php'>enter a unique email";
exit(); // stop script further exec
}
}
}
but "!res" could be for another reason
so I will rather first check if that email exists
then send that message.
start writing it
if you need a hand let us know
It would be worth checking the return values for the mysql_query [uk.php.net] in the manual as you are checking for TRUE...and a query like you are using doesnt return true :(
From the manual -
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE
if ($res === TRUE) {
echo "A record has been inserted";
} else {
echo "Could not insert record:\n".mysql_error();
mysql_close($mysql);
}
if ($res === false) {
echo "This didnt work.\nA team of highly trained monkeys has been dispatched to deal with the situation.";
}
else {
echo 'It worked!';
}
<edit>
Beaten by 3 mins :(
[edited by: PHP_Chimp at 4:53 pm (utc) on Dec. 27, 2007]