harsh00008 Posted July 14, 2008 Share Posted July 14, 2008 I made a simple registering page with username,password,email. the database structure is id>>>bigint>>>lenght/value--255>>>auto_increment>>>primary username>>>varchar>>>lenght--10>>>unique password>>>varchar>>lenght 10>>> email>>>>varchar>>>lenght--20>>>unique i made a test.php it's codes are: <?php $ip=$_SERVER['REMOTE_ADDR']; ?> <center> <form action="register.php" method="post"> <table border=0 cellspacing=0 cellpading=0> <tr> <td>Username:</td><td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td><td> <input type="password" name="password" /></td> </tr> <tr> <td> Confirm Password:</td><td><input type="password" name="pass_conf"/></td> </tr> <tr> <td> Email:</td><td><input type=text name="email"/></td> </tr> <tr> <td> IP:</td><td><input type="hidden" name="ip"><?php echo $ip ?> </td> </tr> </table> <input type="submit" /> </form> </center> and thenn register.php it's codes are: <?php $input="true"; $username=$_POST['username']; $password=$_POST['password']; $pass_conf=$_POST['pass_conf']; $email=$_POST['email']; $null=""; if($username==$null || $password=$null || $email=$null || $pass_conf=$null) { echo "Please fill the form completely in order to proceed!"; $input="false"; }; if($password!=$pass_conf) { echo "Passwords do not match!"; $input="false"; }; $host=localhost; $user = root; $pass = ""; $db = regpage; $connect = mysql_connect( $host, $user, $pass ); if($input="true") { mysql_connect( $host, $user, $pass ) or die(mysql_error()); echo "Successfully connected to server<P>"; mysql_select_db($db) or die(mysql_error()); echo "Connected to the database"; $query="INSERT INTO reg(username,password,email) VALUES('$username','$password','$email')"; mysql_query($query,$connect); if(!mysql_query($query,$connect)) { echo "Sorry!The Registration failed1!"; } else { echo "Thanks for registering!"; }; mysql_close($connect); } else { echo "Sorry!The registration failed2!"; }; ?> The message wwen i get wen i submit the query is: Successfully connected to server Connected to the databaseSorry!The Registration failed1! and the data isn't getting entered into the database plz help Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 You're running the query twice. Try this: <?php $input="true"; $username=$_POST['username']; $password=$_POST['password']; $pass_conf=$_POST['pass_conf']; $email=$_POST['email']; $null=""; if($username==$null || $password=$null || $email=$null || $pass_conf=$null) { echo "Please fill the form completely in order to proceed!"; $input="false"; }; if($password!=$pass_conf) { echo "Passwords do not match!"; $input="false"; }; $host=localhost; $user = root; $pass = ""; $db = regpage; $connect = mysql_connect( $host, $user, $pass ); if($input="true") { mysql_connect( $host, $user, $pass ) or die(mysql_error()); echo "Successfully connected to server<P>"; mysql_select_db($db) or die(mysql_error()); echo "Connected to the database"; $query=mysql_query("INSERT INTO reg(username,password,email) VALUES('$username','$password','$email')") or die("Error: ".mysql_error()); if(!$query) { echo "Sorry!The Registration failed1!"; } else { echo "Thanks for registering!"; }; mysql_close($connect); } else { echo "Sorry!The registration failed2!"; }; ?> I also added an "or die" to check if it is failing, and if it is it'll tell you why. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 it says Error: Duplicate entry '' for key 2 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 What is REG in your query for, I've never seen/used it before. Try removing it, and also emptying your database. Then try again. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 What is REG in your query for, I've never seen/used it before. Try removing it, and also emptying your database. Then try again. well 'reg' is my table name.... now the data is getting entered wen i emptied the data... but the data is being entered only in the 'username' field...rest all the fields are left blank the message now that it shows is Successfully connected to server Connected to the database.Thanks for registering! Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Try this: $query=mysql_query("INSERT INTO `reg` (`username`,`password`,`email`) VALUES ('$username','$password','$email')") or die("Error: ".mysql_error()); Wait, so everything is being put into the 'username' field or only the username is being entered into the database? Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 Try this: $query=mysql_query("INSERT INTO `reg` (`username`,`password`,`email`) VALUES ('$username','$password','$email')") or die("Error: ".mysql_error()); Wait, so everything is being put into the 'username' field or only the username is being entered into the database? Only the username is being entered... by replacing it with your query i get the following error: Successfully connected to server Connected to the database.Error: Duplicate entry '' for key 2 and once again wen i empty the database..only the username is entered... and I get this message: Successfully connected to server Connected to the database.Thanks for registering! Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Well the reason you're getting the duplicate entry error is because the first time, the email is being inputted as blank, then the second time the same thing is happening and because you've set the field to "unique" it'll throw that error at you. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 Well the reason you're getting the duplicate entry error is because the first time, the email is being inputted as blank, then the second time the same thing is happening and because you've set the field to "unique" it'll throw that error at you. but wat about the rest of the fields?? password isn't being entered into the password field in the database.. now wat shall i do??? make both the fields to 'fulltext'?? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 No.. Echo the variables out, above the query to see that they are being set properly. You've got to trace the error down to the source. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 well...yeah all the variables are echoing well and good Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 Show us the structure of your table. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 check it Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 Could you do a: var_dump(); [code] On the section or page where you recieve the POST variables. [/code] Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 yeah i did but sme thing... only the username is getting added in the database.....rest all fields are blank Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 could somebody please run these codes on their local server and then tell me wats wrong ??? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 Well then if they are blank there is obviously a problem with either your forms and how you take in the POST info from those forms OR you are inserting the wrong values into the table. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 Shouldn't this if($input="true") Be if($input=="true") Because if($input="true") is assigning true to $input and not actually comparing them. Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 And you can use: if($input==true) or if($input) if you assign as follows: $input=true; Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 Well then if they are blank there is obviously a problem with either your forms and how you take in the POST info from those forms OR you are inserting the wrong values into the table. hmmm...I guess there's some problem with the query is it??? coz the variables are echoing well :-\ Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 $query=mysql_query("INSERT INTO `reg` (`username`,`password`,`email`) VALUES ('$username','$password','$email')") or die("Error: ".mysql_error()); Looks fine... ??? How about purging the table and then trying the code again? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 14, 2008 Share Posted July 14, 2008 Haha, and check your other variables in your if statement near the top. Your resetting them all. This: if($username==$null || $password=$null || $email=$null || $pass_conf=$null) Should be this: if($username==$null || $password==$null || $email==$null || $pass_conf==$null) Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 Yeap. You have to know the difference between = and ==. == compares two values and = assigns the value to the right onto the value to the left. Quote Link to comment Share on other sites More sharing options...
harsh00008 Posted July 14, 2008 Author Share Posted July 14, 2008 It worked yippie thnx wayn and project Quote Link to comment Share on other sites More sharing options...
waynew Posted July 14, 2008 Share Posted July 14, 2008 It's funny how we all overlook the most simplistic causes. 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.