Akenatehm Posted February 15, 2009 Share Posted February 15, 2009 Hey Guys, I am trying to fix this script: <?php if (isset($_POST['submit'])) { if($_POST['password'] == $_POST['confirmpassword']) { $password = 'Ok'; $dbhost = "localhost"; $dbuser = "username"; $dbpass = "pass"; $dbname = "game"; $connect = mysql_connect($dbhost,$dbuser,$dbpass); if (!$connect) { die('MySQL Error ' . mysql_error()); } else { $selectdb = mysql_select_db($dbname); if (!$selectdb) { die('MySQL Error ' . mysql_error()); } else { $checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'"); if (!$checkuser) { die('MySQL Error' . mysql_error()); } else { if(mysql_num_rows($checkuser) > '1') { $error = '<font color="red"> User Already Exists </font>'; } else { $username = $_POST['username']; $password = $_POST['password']; $password = md5($password); $adduser = mysql_query("INSERT INTO accounts (username, password, last_login) VALUES ('$username' , '$password' , '0')"); if (!$adduser) { die('MySQL Error ' . mysql_error()); } else { $error = '<font color="green">User Added Succesfully </font>'; } } } } } } else { $error = '<font color="red"> Passwords Do Not Match </font>'; } } ?> <html> <head> <title> Game Registration </title> <link rel="stylesheet" href="styles.css" type="text/css"> </head> <body> <center> <table width="300px"> <form action="<?php echo $PHP_SELF; ?>" method="POST"> <tr> <td> Username: </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> Email </td> <td> <input type="text" name="email"> </td> </tr> <tr> <td> Password: </td> <td> <input type="password" name="password"> </td> </tr> <tr> <td> Confirm Password: </td> <td> <input type="password" name="confirmpassword"> </td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Register"> </td> </tr> </form> </table> <p><?php echo $error; ?></p> </center> </body> </html> Which is outputting: User Added Succesfully Even when the user already exists in the Database. It just keeps adding the same username over and over. Help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/ Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Sounds like your database is incorrectly built then. Did you build it with unique and distinct types? Do a show create on your table and show us how it's designed... Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762484 Share on other sites More sharing options...
Akenatehm Posted February 15, 2009 Author Share Posted February 15, 2009 Is this what you need: CREATE TABLE `accounts` ( `playerid` int(11) NOT NULL AUTO_INCREMENT, `username` text NOT NULL, `password` text NOT NULL, `last_login` varchar(255) NOT NULL, PRIMARY KEY (`playerid`) ) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1 Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762487 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Is this what you need: CREATE TABLE `accounts` ( `playerid` int(11) NOT NULL AUTO_INCREMENT, `username` text NOT NULL, `password` text NOT NULL, `last_login` varchar(255) NOT NULL, PRIMARY KEY (`playerid`) ) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1 Yes, there's no unique keyword for your username. Try this: CREATE TABLE `accounts` ( `playerid` int(11) NOT NULL AUTO_INCREMENT, `username` text NOT NULL, `password` text NOT NULL, `last_login` varchar(255) NOT NULL, PRIMARY KEY (`playerid`), UNIQUE KEY playerid (playerid,username) ) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1 Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762488 Share on other sites More sharing options...
Akenatehm Posted February 15, 2009 Author Share Posted February 15, 2009 Ok. Now that i have done this. What will it do? Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762490 Share on other sites More sharing options...
Akenatehm Posted February 15, 2009 Author Share Posted February 15, 2009 Also. When I try to create the new table it says: BLOB/Text column 'username' used in key specification without a key length. Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762492 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Well the purpose is that you are making the playerid and username unique, in order to keep table integrity. You don't want PHP to be the security blanket for checking existing usernames, if somehow someone creates an existing username and passes the PHP test, you want the DB to stop it at its tracks. Also, why are you using "Text" for username? Use varchar or char.... `username` varchar(32) NOT NULL You don't want your username to be as long as whatever anyone wants. Make it either 32 or 50 characters. Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762493 Share on other sites More sharing options...
Akenatehm Posted February 15, 2009 Author Share Posted February 15, 2009 Ok, it worked now. But I still have the problem that PHP doesnt know when to say that the username already exists... Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762495 Share on other sites More sharing options...
Philip Posted February 15, 2009 Share Posted February 15, 2009 You're checking if it's greater than one row, not equal to: if(mysql_num_rows($checkuser) > '1') should be: if(mysql_num_rows($checkuser) > 0) Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762498 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Because you're not assigning $username to anything. $checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'"); So make it: $checkuser = mysql_query("SELECT * FROM accounts WHERE username = '".$_POST[username]."' "); Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762499 Share on other sites More sharing options...
Akenatehm Posted February 15, 2009 Author Share Posted February 15, 2009 Omg i cant believe it was something as stupid as that...thanks for ur help. Quote Link to comment https://forums.phpfreaks.com/topic/145253-solved-checking-if-username-already-exists-in-the-db/#findComment-762504 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.