duffman014 Posted July 27, 2009 Share Posted July 27, 2009 <? $host="cndlogin.db.asdf.com"; // Host name $username="cin"; // Mysql username $password="sorry"; // Mysql password $db_name="casdfogin"; // Database name $tbl_name="login"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Get values from form $uname=$_POST['uname']; $pw=$_POST['pw']; $vpw=$_POST['vpw']; $email=$_POST['email']; $ip=$_POST['ip']; // Insert data into mysql $sql="INSERT INTO $tbl_name(uname, pw, email, ip)VALUES('$uname', '$pw', '$email', 'ip')"; $result=mysql_query($sql); if($name == false || $pass == false || $email == false) { echo "Please fill in all the required fields."; }; if($pass != $pass_conf){ echo "Passwords do not match."; } else { echo "thank you for your registration to cndalliance.com"; mysql_close(); } ?> Ok here the code i've come up wtih thus far... i'm trying to make an if statment that will check if the username has been taken???? i just cant figure it out i've seen some examples i just cant encorporate thema nd put them all togeather... any ideas or help greatley appreceated... Thx, Quote Link to comment https://forums.phpfreaks.com/topic/167598-how-to-make-if-statment-for-registration-page/ Share on other sites More sharing options...
GingerRobot Posted July 27, 2009 Share Posted July 27, 2009 Well what have you tried? The basic process is to select the rows from the database where the username is equal to the one the current user is trying to register. You then take a count of the rows. If it is 0, the username's not been taken. If it is 1, it has. Quote Link to comment https://forums.phpfreaks.com/topic/167598-how-to-make-if-statment-for-registration-page/#findComment-883792 Share on other sites More sharing options...
duffman014 Posted July 27, 2009 Author Share Posted July 27, 2009 Well what have you tried? The basic process is to select the rows from the database where the username is equal to the one the current user is trying to register. You then take a count of the rows. If it is 0, the username's not been taken. If it is 1, it has. $query = "SELECT * FROM UserTable WHERE username=".$_Post['username']; $Con = mysql_query($query, $connection) or die(mysql_error()); $row = mysql_fetch_assoc($Con); $totalRows = mysql_num_rows($Con); if($totalRows>0) { echo 'Username exist'; } else { echo 'Unique Username'; } I tried using this but i could never get it right... i Quote Link to comment https://forums.phpfreaks.com/topic/167598-how-to-make-if-statment-for-registration-page/#findComment-883793 Share on other sites More sharing options...
GingerRobot Posted July 27, 2009 Share Posted July 27, 2009 If that's your exact code then your main problem is with the first line: $query = "SELECT * FROM UserTable WHERE username=".$_Post['username']; You've not placed the username that you're searching for in quotes, which is something you must do for all strings in your query. A cleaner version would be: $sql = "SELECT COUNT(*) FROM UserTable WHERE username= '" . $_POST['username'] . "'"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); if($result){ if(mysql_result($result,0) == 0){ //username not taken }else{ //username taken } }else{ //problem with query } Quote Link to comment https://forums.phpfreaks.com/topic/167598-how-to-make-if-statment-for-registration-page/#findComment-883811 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.