namasteji1 Posted August 19, 2012 Share Posted August 19, 2012 <?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! Quote Link to comment Share on other sites More sharing options...
floridaflatlander Posted August 19, 2012 Share Posted August 19, 2012 Before the INSERT do a SELECT that looks for one or more of something, ie an username, email, location or all or more. Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 Can you please give an example, like checking for duplicate email address? Thanks Quote Link to comment Share on other sites More sharing options...
floridaflatlander Posted August 19, 2012 Share Posted August 19, 2012 $q = "SELECT id FROM users WHERE email='$e'"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 0) { // Available. run insert } else{ echo this email is taken} Also and this is a big also you need to clean/secure your data before you put it in the db table. Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 $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] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 mysql_* extension functions and mysqli_* extension functions aren't directly interchangeable. You need to use one or the other. Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 mysql_* extension functions and mysqli_* extension functions aren't directly interchangeable. You need to use one or the other. Ok, i used mysql only and the same error is displaying. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 That is impossible. Make sure you saved the file, and that the error is actually identical. Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 Here is the error i am getting now- Quote Link to comment Share on other sites More sharing options...
floridaflatlander Posted August 19, 2012 Share Posted August 19, 2012 What line number is the error on? Look at the line number and see what's wrong. Compare that to the input query Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 The syntax is somewhat different between the two sets of functions also. mysql_query mysqli_query mysql_num_rows mysqli_num_rows Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 I am stuck and unable to find out what to do Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 19, 2012 Share Posted August 19, 2012 I am stuck and unable to find out what to do http://php.net/manual/en/function.mysql-connect.php http://www.php.net/manual/en/function.mysql-select-db.php You need to use those two first. The first one will return the "resource" mysql_query request a second parameter to be. Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 19, 2012 Author Share Posted August 19, 2012 I have used them at the start of my code as you can see. So there's not a problem with that. Quote Link to comment Share on other sites More sharing options...
aliento Posted August 19, 2012 Share Posted August 19, 2012 <?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"; ?> Quote Link to comment Share on other sites More sharing options...
namasteji1 Posted August 21, 2012 Author Share Posted August 21, 2012 <?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 ? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 21, 2012 Share Posted August 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
aliento Posted August 21, 2012 Share Posted August 21, 2012 <?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"; ?> Quote Link to comment Share on other sites More sharing options...
aliento Posted August 21, 2012 Share Posted August 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 21, 2012 Share Posted August 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
aliento Posted August 21, 2012 Share Posted August 21, 2012 Quote Link to comment Share on other sites More sharing options...
aliento Posted August 21, 2012 Share Posted August 21, 2012 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 ! Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 21, 2012 Share Posted August 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 21, 2012 Share Posted August 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
aliento Posted August 21, 2012 Share Posted August 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.