Ryuujin Posted May 28, 2008 Share Posted May 28, 2008 I have this registration script, and it just isn't working. See anything wrong? else if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $password_verify = mysql_real_escape_string($_POST['password_verify']); $password_encrypt = md5($password); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if(isset($username)) { if(isset($password)) { if(isset($password_verify)) { if($password == "$password_verify") { if(isset($email)) { if(isset($kingdom_name)) { if(isset($server)) { mysql_query("SELECT * FROM Users WHERE Username='$username'"); $username_check = mysql_affected_rows(); if($username_check === 0) { mysql_query("SELECT * FROM Users WHERE Email='$email'"); $email_check = mysql_affected_rows(); if($email_check === 0) { mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $kingdom_name_check = mysql_affected_rows(); if($kingdom_name_check === 0) { mysql_query("INSERT INTO Users (`Username`, `Password`, `Ip`, `Joined`, `Email`) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')"); $user_success = mysql_affected_rows(); mysql_query("INSERT INTO Kingdoms (`Kingdom_Owner`, `Kingdom_Name`, `Kingdom_Gold`, `Kingdom_Turns`, `Kingdom_GoldMine_Level`, `Kingdom_Barracks_Level`, `Kingdom_TownCenter_Level`, `Kingdom_Spies_Count`, `Kingdom_Guards_Count`, `Kingdom_Swordsmen_Count`, `Kingdom_Macemen_Count`, `Kingdom_Spearmen_Count`, `Kingdom_Archers_Count`, `Kingdom_Server`) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')"); $kingdom_success = mysql_affected_rows(); if($user_success === 1) { if($kingdom_success === 1) { echo '<td>User has been inserted into the database successfully.<br />Kingdom has been inserted into the database successfully.<br /><br />Registration Successful, please <a href="login.php">Login.</a></td>'; } else { echo '<td>Kingdom was not inserted into the database.<br /></td>'; } } else { echo '<td>User was not inserted into the database.<br /></td>'; } } else { echo '<td>Kingdom name already exists!<br /></td>'; } } else { echo '<td>Email already exists!<br /></td>'; } } else { echo '<td>Username already exists!<br /></td>'; } } else { echo '<td>Please choose a server.<br /></td>'; } } else { echo '<td>Please enter a name for your kingdom.<br /></td>'; } } else { echo '<td>Please enter your email.<br /></td>'; } } else { echo '<td>The passwords do not match.<br /></td>'; } } else { echo '<td>Please verify your password.<br /></td>'; } } else { echo '<td>Please enter your password.<br /></td>'; } } else { echo '<td>Please enter your username.<br /></td>'; } echo '</tr> </table>'; } Thanks, -Ryu Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/ Share on other sites More sharing options...
Ne.OnZ Posted May 28, 2008 Share Posted May 28, 2008 Woah Guy, you should really condense your code. $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $password_verify = mysql_real_escape_string($_POST['password_verify']); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; All those if statements could go into 1 line: if($username && $password && $password_verify == $password && $email && $kingdom_name && $server) . You should simplify your code, since it's quite annoying to look at. Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552125 Share on other sites More sharing options...
Ryuujin Posted May 28, 2008 Author Share Posted May 28, 2008 It COULD go into one line. But I didn't because it is easier for me to edit that way, and I can put errors to each thing. Simple as that. -Ryu Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552145 Share on other sites More sharing options...
Cory94bailly Posted May 28, 2008 Share Posted May 28, 2008 How "Isn't it working"..? What's the error(s)? Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552150 Share on other sites More sharing options...
Ryuujin Posted May 29, 2008 Author Share Posted May 29, 2008 It gives me the "Username already exists!" error. Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552244 Share on other sites More sharing options...
.josh Posted May 29, 2008 Share Posted May 29, 2008 I'm sure it's probably an easy fix but nobody wants to look at code like that. If it works for you, then more power to you, but don't expect people to go rushing in to help you with it looking like that. Simple as that. -Crayon Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552254 Share on other sites More sharing options...
Cory94bailly Posted May 29, 2008 Share Posted May 29, 2008 Umm looking at it, it looks really wrong in my eyes.. I may not know much php but that script seems wrong.. Shouldn't it be like this: if(isset($username)) { echo '<td>User was not inserted into the database.<br /></td>'; } I'm probably wrong but trying to help. Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552257 Share on other sites More sharing options...
prcollin Posted May 29, 2008 Share Posted May 29, 2008 try this else if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $password_verify = mysql_real_escape_string($_POST['password_verify']); $password_encrypt = md5($password); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if(isset($username)) { if(isset($password)) { if(isset($password_verify)) { if($password == "$password_verify") { if(isset($email)) { if(isset($kingdom_name)) { if(isset($server)) { mysql_query("SELECT * FROM Users WHERE Username='$username'"); $username_check = mysql_affected_rows(); if($username_check === 0) { mysql_query("SELECT * FROM Users WHERE Email='$email'"); $email_check = mysql_affected_rows(); if($email_check === 0) { mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $kingdom_name_check = mysql_affected_rows(); if($kingdom_name_check === 0) { mysql_query("INSERT INTO Users (`Username`, `Password`, `Ip`, `Joined`, `Email`) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')"); $user_success = mysql_affected_rows(); mysql_query("INSERT INTO Kingdoms (`Kingdom_Owner`, `Kingdom_Name`, `Kingdom_Gold`, `Kingdom_Turns`, `Kingdom_GoldMine_Level`, `Kingdom_Barracks_Level`, `Kingdom_TownCenter_Level`, `Kingdom_Spies_Count`, `Kingdom_Guards_Count`, `Kingdom_Swordsmen_Count`, `Kingdom_Macemen_Count`, `Kingdom_Spearmen_Count`, `Kingdom_Archers_Count`, `Kingdom_Server`) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')"); $kingdom_success = mysql_affected_rows(); if($user_success == 1) { if($kingdom_success == 1) { echo '<td>User has been inserted into the database successfully.<br />Kingdom has been inserted into the database successfully.<br /><br />Registration Successful, please <a href="login.php">Login.</a></td>'; } else { echo '<td>Kingdom was not inserted into the database.<br /></td>'; } } else { echo '<td>User was not inserted into the database.<br /></td>'; } } else { echo '<td>Kingdom name already exists!<br /></td>'; } } else { echo '<td>Email already exists!<br /></td>'; } } else { echo '<td>Username already exists!<br /></td>'; } } else { echo '<td>Please choose a server.<br /></td>'; } } else { echo '<td>Please enter a name for your kingdom.<br /></td>'; } } else { echo '<td>Please enter your email.<br /></td>'; } } else { echo '<td>The passwords do not match.<br /></td>'; } } else { echo '<td>Please verify your password.<br /></td>'; } } else { echo '<td>Please enter your password.<br /></td>'; } } else { echo '<td>Please enter your username.<br /></td>'; } echo '</tr> </table>'; } Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552258 Share on other sites More sharing options...
unidox Posted May 29, 2008 Share Posted May 29, 2008 Confusing as hell to fix, but here: <?php if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $password_verify = mysql_real_escape_string($_POST['password_verify']); $password_encrypt = md5($password); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if(isset($username)) { if(isset($password)) { if(isset($password_verify)) { if($password == $password_verify) { if(isset($email)) { if(isset($kingdom_name)) { if(isset($server)) { $q = mysql_query("SELECT * FROM Users WHERE Username='$username'"); $row = mysql_num_rows($q); if($row > 0) { $q = mysql_query("SELECT * FROM Users WHERE Email='$email'"); $row = mysql_num_rows($q); if($row > 0) { $q = mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $row = mysql_num_rows($q); if($row > 0) { mysql_query("INSERT INTO Users (`Username`, `Password`, `Ip`, `Joined`, `Email`) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')") or die(mysql_error()); $user_success = true; mysql_query("INSERT INTO Kingdoms (`Kingdom_Owner`, `Kingdom_Name`, `Kingdom_Gold`, `Kingdom_Turns`, `Kingdom_GoldMine_Level`, `Kingdom_Barracks_Level`, `Kingdom_TownCenter_Level`, `Kingdom_Spies_Count`, `Kingdom_Guards_Count`, `Kingdom_Swordsmen_Count`, `Kingdom_Macemen_Count`, `Kingdom_Spearmen_Count`, `Kingdom_Archers_Count`, `Kingdom_Server`) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')") or die(mysql_error()); $kingdom_success = true; if($user_success) { if($kingdom_success) { echo '<td>User has been inserted into the database successfully.<br />Kingdom has been inserted into the database successfully.<br /><br />Registration Successful, please <a href="login.php">Login.</a></td>'; } else { echo '<td>Kingdom was not inserted into the database.<br /></td>'; } } else { echo '<td>User was not inserted into the database.<br /></td>'; } } else { echo '<td>Kingdom name already exists!<br /></td>'; } } else { echo '<td>Email already exists!<br /></td>'; } } else { echo '<td>Username already exists!<br /></td>'; } } else { echo '<td>Please choose a server.<br /></td>'; } } else { echo '<td>Please enter a name for your kingdom.<br /></td>'; } } else { echo '<td>Please enter your email.<br /></td>'; } } else { echo '<td>The passwords do not match.<br /></td>'; } } else { echo '<td>Please verify your password.<br /></td>'; } } else { echo '<td>Please enter your password.<br /></td>'; } } else { echo '<td>Please enter your username.<br /></td>'; } echo '</tr> </table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552259 Share on other sites More sharing options...
Ryuujin Posted May 29, 2008 Author Share Posted May 29, 2008 Umm looking at it, it looks really wrong in my eyes.. I may not know much php but that script seems wrong.. Shouldn't it be like this: if(isset($username)) { echo '<td>User was not inserted into the database.<br /></td>'; } I'm probably wrong but trying to help. Yes that is wrong. Basically, if the username is set, it would echo that the thing was not entered into the database. I want it to say that if it does NOT get entered into the database. Thanks for the effort though... try this else if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $password_verify = mysql_real_escape_string($_POST['password_verify']); $password_encrypt = md5($password); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if(isset($username)) { if(isset($password)) { if(isset($password_verify)) { if($password == "$password_verify") { if(isset($email)) { if(isset($kingdom_name)) { if(isset($server)) { mysql_query("SELECT * FROM Users WHERE Username='$username'"); $username_check = mysql_affected_rows(); if($username_check === 0) { mysql_query("SELECT * FROM Users WHERE Email='$email'"); $email_check = mysql_affected_rows(); if($email_check === 0) { mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $kingdom_name_check = mysql_affected_rows(); if($kingdom_name_check === 0) { mysql_query("INSERT INTO Users (`Username`, `Password`, `Ip`, `Joined`, `Email`) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')"); $user_success = mysql_affected_rows(); mysql_query("INSERT INTO Kingdoms (`Kingdom_Owner`, `Kingdom_Name`, `Kingdom_Gold`, `Kingdom_Turns`, `Kingdom_GoldMine_Level`, `Kingdom_Barracks_Level`, `Kingdom_TownCenter_Level`, `Kingdom_Spies_Count`, `Kingdom_Guards_Count`, `Kingdom_Swordsmen_Count`, `Kingdom_Macemen_Count`, `Kingdom_Spearmen_Count`, `Kingdom_Archers_Count`, `Kingdom_Server`) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')"); $kingdom_success = mysql_affected_rows(); if($user_success == 1) { if($kingdom_success == 1) { echo '<td>User has been inserted into the database successfully.<br />Kingdom has been inserted into the database successfully.<br /><br />Registration Successful, please <a href="login.php">Login.</a></td>'; } else { echo '<td>Kingdom was not inserted into the database.<br /></td>'; } } else { echo '<td>User was not inserted into the database.<br /></td>'; } } else { echo '<td>Kingdom name already exists!<br /></td>'; } } else { echo '<td>Email already exists!<br /></td>'; } } else { echo '<td>Username already exists!<br /></td>'; } } else { echo '<td>Please choose a server.<br /></td>'; } } else { echo '<td>Please enter a name for your kingdom.<br /></td>'; } } else { echo '<td>Please enter your email.<br /></td>'; } } else { echo '<td>The passwords do not match.<br /></td>'; } } else { echo '<td>Please verify your password.<br /></td>'; } } else { echo '<td>Please enter your password.<br /></td>'; } } else { echo '<td>Please enter your username.<br /></td>'; } echo '</tr> </table>'; } Didn't work. I'm sure it's probably an easy fix but nobody wants to look at code like that. If it works for you, then more power to you, but don't expect people to go rushing in to help you with it looking like that. Simple as that. -Crayon Well, then please show me how you would code it. I am always welcome to improving my coding abilities. Confusing as hell to fix, but here: <?php if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $password_verify = mysql_real_escape_string($_POST['password_verify']); $password_encrypt = md5($password); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if(isset($username)) { if(isset($password)) { if(isset($password_verify)) { if($password == $password_verify) { if(isset($email)) { if(isset($kingdom_name)) { if(isset($server)) { $q = mysql_query("SELECT * FROM Users WHERE Username='$username'"); $row = mysql_num_rows($q); if($row > 0) { $q = mysql_query("SELECT * FROM Users WHERE Email='$email'"); $row = mysql_num_rows($q); if($row > 0) { $q = mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $row = mysql_num_rows($q); if($row > 0) { mysql_query("INSERT INTO Users (`Username`, `Password`, `Ip`, `Joined`, `Email`) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')") or die(mysql_error()); $user_success = true; mysql_query("INSERT INTO Kingdoms (`Kingdom_Owner`, `Kingdom_Name`, `Kingdom_Gold`, `Kingdom_Turns`, `Kingdom_GoldMine_Level`, `Kingdom_Barracks_Level`, `Kingdom_TownCenter_Level`, `Kingdom_Spies_Count`, `Kingdom_Guards_Count`, `Kingdom_Swordsmen_Count`, `Kingdom_Macemen_Count`, `Kingdom_Spearmen_Count`, `Kingdom_Archers_Count`, `Kingdom_Server`) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')") or die(mysql_error()); $kingdom_success = true; if($user_success) { if($kingdom_success) { echo '<td>User has been inserted into the database successfully.<br />Kingdom has been inserted into the database successfully.<br /><br />Registration Successful, please <a href="login.php">Login.</a></td>'; } else { echo '<td>Kingdom was not inserted into the database.<br /></td>'; } } else { echo '<td>User was not inserted into the database.<br /></td>'; } } else { echo '<td>Kingdom name already exists!<br /></td>'; } } else { echo '<td>Email already exists!<br /></td>'; } } else { echo '<td>Username already exists!<br /></td>'; } } else { echo '<td>Please choose a server.<br /></td>'; } } else { echo '<td>Please enter a name for your kingdom.<br /></td>'; } } else { echo '<td>Please enter your email.<br /></td>'; } } else { echo '<td>The passwords do not match.<br /></td>'; } } else { echo '<td>Please verify your password.<br /></td>'; } } else { echo '<td>Please enter your password.<br /></td>'; } } else { echo '<td>Please enter your username.<br /></td>'; } echo '</tr> </table>'; } ?> Still no results but that is a good way to handle it. Thanks for all the help so far, -Ryu Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552263 Share on other sites More sharing options...
unidox Posted May 29, 2008 Share Posted May 29, 2008 Well, 1st, use functions to handle xss and sql prevention. www.gigaspartan.com has a login/registration tutorial I made that you can easily build off of. Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552266 Share on other sites More sharing options...
Ne.OnZ Posted May 29, 2008 Share Posted May 29, 2008 Well, then please show me how you would code it. I am always welcome to improving my coding abilities. I would code it like this: else if($action == "Register") { $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $password_verify = md5(mysql_real_escape_string($_POST['password_verify'])); $email = mysql_real_escape_string($_POST['email']); $kingdom_name = mysql_real_escape_string($_POST['kingdom_name']); $server = mysql_real_escape_string($_POST['server']); $joined = date('m:d:y'); $ip = $_SERVER['REMOTE_ADDR']; echo '<table border="1" width="100%"> <tr> <th>Registration Progress</th> </tr> <tr>'; if($username && $password && $password == $password_verify && $email && $kingdom_name && $server) { mysql_query("SELECT * FROM Users WHERE Username='$username'"); $username_check = mysql_affected_rows(); if($username_check == 0) { mysql_query("SELECT * FROM Users WHERE Email='$email'"); $email_check = mysql_affected_rows(); } if($email_check == 0) { mysql_query("SELECT * FROM Kingdoms WHERE Kingdom_Name = $kingdom_name"); $kingdom_name_check = mysql_affected_rows(); } if($kingdom_name_check == 0) { mysql_query("INSERT INTO Users (Username, Password, Ip, Joined, Email) VALUES ('$username', '$password', '$ip', '$joined', '$email')"); $user_success = mysql_affected_rows(); mysql_query("INSERT INTO Kingdoms (Kingdom_Owner, Kingdom_Name, Kingdom_Gold, Kingdom_Turns, Kingdom_GoldMine_Level, Kingdom_Barracks_Level, Kingdom_TownCenter_Level, Kingdom_Spies_Count, Kingdom_Guards_Count, Kingdom_Swordsmen_Count, Kingdom_Macemen_Count, Kingdom_Spearmen_Count, Kingdom_Archers_Count, Kingdom_Server) VALUES ('$username', '$kingdom_name', '5000', '50', '1', '1', '1', '0', '10', '0', '0', '0', '0', '$server')"); $kingdom_success = mysql_affected_rows(); } if($user_success == 1 && $kingdom_success == 1) { echo '<td>User has been inserted into the database successfully.<br /> Kingdom has been inserted into the database successfully.<br /><br /> Registration Successful, please <a href="login.php">Login.</a></td>'; } } else if(!isset($username)) { echo "Please enter a username"; } else if(!isset($password)) { echo "Please enter a password"; } else if($password != $password_verify) { echo "Your passwords do not match"; } else if(!isset($email)) { echo "Please enter an e-mail address"; } else if(!isset($kingdom_name)) { echo "Please enter a Kingdom Name"; } else if(!isset($server)) { echo "Please select a server"; } echo '</tr> </table>'; } NOTE: I didn't check for errors, I just cleaned it up as I would do it. This may not be the best way to write it out, but it is a little easier to read, don't you think? Sorry if I made any mistakes anywhere... Just a few things I noticed: You have 3 equal signs in some places (===). I believe you only need 2. I've never seen 3 before. Also, the $password_encrypt is not needed. You can put md5(mysql_real_escape_string($_POST['password'])); it works the same. On this part: mysql_query("INSERT INTO Users (Username, Password, Ip, Joined, Email) VALUES ('$username', '$password_encrypt', '$ip', '$joined', '$email')"); You had "`" in the field names (ex. `Username`, `Password`), still here the "`" is not really needed. Also, for each query you do, you should store them in variables, then call it from another variable and have OR die(mysql_error()); Example: $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query($sql) OR die(mysql_error()); With the function mysql_error() being there, you can see if anything was wrong with that query or not. Remember this is just an example, please adapt it to your needs. Please correct me if I'm wrong anywhere, as I am not a php wiz. Quote Link to comment https://forums.phpfreaks.com/topic/107699-registration-script/#findComment-552294 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.