Jump to content

How to check duplicate entry in registration form?


namasteji1

Recommended Posts

<?php
session_start();session_destroy();
session_start();
if($_POST["regname"] && $_POST["regemail"]
&& $_POST["regmobile"] && $_POST["regpass1"] && $_POST["regpass2"] )

{
if($_POST["regpass1"]==$_POST["regpass2"])
{
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    



$sql="insert into users (name,email,mobile,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regmobile]','$_POST[regpass1]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());		

    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
}

else print "Passwords doesn't match";
}
else print"Invalid input data";

?>

 

 

 

I have email set as unique index in my table in database. I want to display error message for duplicate entries a user may make while registering. Please help!

Link to comment
Share on other sites


$q = "SELECT id FROM users WHERE email='$_POST[regemail]'";

$r = mysqli_query ($dbc, $q);           // Line 39

if (mysqli_num_rows($r) == 0) { // Available.      // Line 41






$sql="insert into users (name,email,mobile,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regmobile]','$_POST[regpass1]')";

}else print "this email is taken";

    $result=mysql_query($sql,$conn) or die(mysql_error());		

    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
}


else print "Passwords doesn't match";
}
else print"Invalid input data";

?>

 

So i did changes as you told but i am getting the following message[/code]

post-135859-13482403716904_thumb.png

Link to comment
Share on other sites

<?php
session_start();session_destroy();
session_start();



if($_POST["regname"] && $_POST["regemail"]
&& $_POST["regmobile"] && $_POST["regpass1"] && $_POST["regpass2"] )

{
if($_POST["regpass1"]==$_POST["regpass2"])
{
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    

$sql = "SELECT `id` from users where `email`= '".$_POST[regemail]."' LIMIT 1";
   $result=mysql_query($sql,$conn) or die(mysql_error());		
$num_rows = mysql_num_rows($result);
if($num_rows==0) 
{
$sql="insert into users (name,email,mobile,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regmobile]','$_POST[regpass1]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());		

    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
}
else echo "duplicated data";
}

else print "Passwords doesn't match";
}
else print"Invalid input data";

?>

Link to comment
Share on other sites

<?php
session_start();session_destroy();
session_start();



if($_POST["regname"] && $_POST["regemail"]
&& $_POST["regmobile"] && $_POST["regpass1"] && $_POST["regpass2"] )

{
if($_POST["regpass1"]==$_POST["regpass2"])
{
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    

$sql = "SELECT `id` from users where `email`= '".$_POST[regemail]."' LIMIT 1";
   $result=mysql_query($sql,$conn) or die(mysql_error());		
$num_rows = mysql_num_rows($result);
if($num_rows==0) 
{
$sql="insert into users (name,email,mobile,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regmobile]','$_POST[regpass1]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());		

    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
}
else echo "duplicated data";
}

else print "Passwords doesn't match";
}
else print"Invalid input data";

?>

 

 

Many thanks for this, but this seems to check for any duplicate data i think? It's not specifically for duplicate email id ? I tried to add check for duplicate username by using your same code above and replacing email with username but , after registering the message appears of duplicate data again. Can you give code for checking duplicate username ?

Link to comment
Share on other sites

The SQL to check for the e-mail is not quite right:

 

$sql = "SELECT `id` from users where `email`= '".$_POST[regemail]."' LIMIT 1";

 

Should be:

 

$sql = "SELECT `id` from users where `email`= '$_POST[regemail]' LIMIT 1";

 

To check for the username, do the same thing:

 

$sql = "SELECT `id` from users where `name`= '$_POST[regname]' LIMIT 1";

 

But remember you need another set of if/else in case of a duplicate.

Link to comment
Share on other sites

<?php
session_start();session_destroy();
session_start();



if($_POST["regname"] && $_POST["regemail"]
&& $_POST["regmobile"] && $_POST["regpass1"] && $_POST["regpass2"] )

{
if($_POST["regpass1"]==$_POST["regpass2"])
{
$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("test",$conn);
    

$sql = "SELECT `id` from users where `email`= ".$_POST['regemail']."' LIMIT 1";
   $result=mysql_query($sql,$conn) or die(mysql_error());		
$mail_rows = mysql_num_rows($result);
$sql = "SELECT `id` from users where `regname`= '".$_POST['regname']."' LIMIT 1";
   $result=mysql_query($sql,$conn) or die(mysql_error());		
$name_rows = mysql_num_rows($result);
if($mail_rows==0&&$name_rows==0) 
{
$sql="insert into users (name,email,mobile,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regmobile]','$_POST[regpass1]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());		

    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
}
else echo "duplicated data";
}

else print "Passwords doesn't match";
}
else print"Invalid input data";

?>

Link to comment
Share on other sites

The SQL to check for the e-mail is not quite right:

 

$sql = "SELECT `id` from users where `email`= '".$_POST[regemail]."' LIMIT 1";

 

Should be:

 

$sql = "SELECT `id` from users where `email`= '$_POST[regemail]' LIMIT 1";

 

To check for the username, do the same thing:

 

$sql = "SELECT `id` from users where `name`= '$_POST[regname]' LIMIT 1";

 

But remember you need another set of if/else in case of a duplicate.

 

if it was a single variable its as you say because is an array element should be separated from the query

Link to comment
Share on other sites

The SQL to check for the e-mail is not quite right:

 

$sql = "SELECT `id` from users where `email`= '".$_POST[regemail]."' LIMIT 1";

 

Should be:

 

$sql = "SELECT `id` from users where `email`= '$_POST[regemail]' LIMIT 1";

 

To check for the username, do the same thing:

 

$sql = "SELECT `id` from users where `name`= '$_POST[regname]' LIMIT 1";

 

But remember you need another set of if/else in case of a duplicate.

 

if it was a single variable its as you say because is an array element should be separated from the query

 

Actually, mine works just fine. I'm personally not a big fan of concatenating a variable to a double quoted string since it's just extra work, but both your way and my way will work.  :)

Link to comment
Share on other sites

further more see a $string = 'bla bla bla : $array['whatever'] '; Then it will stop the string at the [' because is the end of the string !

 

That's true, and for single quotes you have to concatenate anyway, but:

 

$stuff = array('fruit_1' => 'apple');
echo "I want an $stuff[fruit_1]"; //Echoes: I want an apple
echo "I want an ".$stuff['fruit_1']; //Echoes: I want an apple

Link to comment
Share on other sites

Actually, mine works just fine. I'm personally not a big fan of concatenating a variable to a double quoted string since it's just extra work, but both your way and my way will work.  :)

 

aliento's way may work, but it isn't right at all. For every array element broken out of the string, with the index unquoted, it generates an 'undefined constant' notice. Then if there happens to be a constant defined with the same name as the array index, that would probably crater the whole thing.

 

echo "I want an $stuff[fruit_1]"; is just fine because the index is within a double-quoted string, although for consistency and clarity may be better written as:

echo "I want an {$stuff['fruit_1']}"; which is complex notation.

 

echo "I want an " .  $stuff[fruit_1]; will cause an 'undefined constant' notice because when left unquoted, the array index is first interpreted as a constant, then if no constant is defined by that name, php assumes you meant to quote it to have it interpreted as a string. If you have error reporting set up properly while developing, you'll see all of those notices.

 

 

Link to comment
Share on other sites

When the index into an array is number can be used as $something[1] if it is string $something['other']. The $something[other] just now i show it and i disagree, i think is a bug. The (string) is different than (int) and somehow i should know the type when i program.

 

About the string this is ok : $string = "bla bla bla $dolphin"; or "bla bla bla $dolphin['first']"

But when i should put the string into a function do_something("bla bla bla $dolphin['first']") is false and will return error but do_something("bla bla bla ".$dolphin['first'].") is ok.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.