I have the code below to output the text if username already exist
but it seems to output the mysql error code instead “Duplicate entry ‘Mike’ for key ‘UserName’”
<html>
<head>
<link rel="stylesheet" type="text/css" href="../121style.css" />
</head>
<body>
<?php
include "../config/seperatedb.php";
$loginname = $_POST['loginname'];
$loginname2 = ucwords($loginname);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$country = $_POST['country'];
$occupation = $_POST['occupation'];
$yearofbirth = $_POST['yearofbirth'];
$password = $_POST['password'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$martialstatus = $_POST['martialstatus'];
$randomnumber1= rand(0,9);
$randomnumber2= rand(0,9);
$randomnumber3= rand(0,9);
$randomnumber4= rand(0,9);
$regdate=date("d/m/Y");
$ipreg=$_SERVER['REMOTE_ADDR'];
$query = "SELECT
UserName
FROM
member
WHERE
UserName ='$loginname2'";
$result = mysql_query($query);
$query3 = mysql_query("SELECT * FROM member WHERE UserName='$loginname' ")
or die("Could not insert data because ".mysql_error());
$qry3 = mysql_fetch_array( $query3 );
?>
<img src='../images/sublogos/suserbanner.gif'><br>
<?php
if (strlen($loginname2) < 1 || strlen($password) < 3 || strlen($loginname2) > 14 || strlen($firstname) < 3 || strlen($lastname) < 3 || strlen($occupation) < 3 || strlen($email) < 3 || strlen($yearofbirth) < 4 || !($qry3['UserName']))
{
?>
<b>Error</b><br>Your account could not be created because 1 of these errors below:<br></br>
<li>The username you entered may already exist<br>
<li>Your username was not entered<br>
<li>Your username cannot be more than 14 characters<br>
<li>Your password must be a minimum of 3 characters<br>
<li>Your firstname and last name must be more than 3 characters<br>
<li>Your occupation must be more than 3 characters<br>
<li>Your email must be more than 3 characters<br>
<li>Your year of birth must be 4 characters<br></br>
<a href='register.php'>Click here</a> to try again.
<?php
}
$request = "INSERT INTO `member` ( `UserName` , `firstname` , `lastname` , `country` , `yob` , `gender` , `email` , `Password` , `MemberType` , `ActivateCode` , `Activated` , `registrationdate` , `occupation` , `MaritalStatus` , `RegIP` )
VALUES (
'$loginname2' , '$firstname' , '$lastname' , '$country' , '$yearofbirth' , '$gender' , '$email' , '$password' , '1' , '$randomnumber1$randomnumber2$randomnumber3$randomnumber4' , '0' , '$regdate' , '$occupation' , '$martialstatus' , '$ipreg');";
// execute the query
$result = mysql_query($request) or die(mysql_error());
echo "Your account has been created!<br></br>Before you can login to your account you must activate. Please check for email for your activation code.";
$to = "$email";
$subject = "121ChatRooms Membership Activation";
$body = "Dear $loginname2,
This is an automated email message from 121ChatRooms Member Administration, which provides you with your membership information and how to activate your account.
UserName: $loginname2
Activation Code: $randomnumber1$randomnumber2$randomnumber3$randomnumber4
You will be able to login using this username after you have activated your account.
To activate your account you will need to click the link below:
www.121chatrooms.net/index.php?loginname=$loginname2&code=$randomnumber1$randomnumber2$randomnumber3$randomnumber4
If you do not activate your account within 5 days your membership will be deleted.
Many happy hours on 121ChatRooms,
121ChatRooms Member Administration
support@121chatrooms.net
www.121chatrooms.net";
if (mail($to, $subject, $body)) {
} else {
echo("Message delivery failed");
}
?>
<br><p><table border="0" cellspacing="0" cellpadding="0" height="20" id="table1">
<tr>
<td width='87.5' class='btmbtn' align='center'><b></b></td>
<td width='87.5' class='btmbtn' align='center'><b></b></td>
<td width='87.5' class='btmbtn' align='center'><b></b></td>
<td width='87.5' class='btmbtn' align='center'><b></b></td>
</tr>
</table>
</body>
</html>
seeing you want to check if a username exists or not, just check if the the number of rows returned by your query = 1 or not and then set a boolean variable to either true or false and then you can use that boolean in your if statement.