Jump to content

How to make if statment for registration page.....


duffman014

Recommended Posts

<?
$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,

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.