Mysql error

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’”

is there a way to fix this?


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'])) 
{

would really need to see the rest of the if block code and where $qry3[‘UserName’] is being assigned a value before the if block.


&lt;html&gt;
&lt;head&gt;
&lt;link rel="stylesheet" type="text/css" href="../121style.css" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?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 );
?&gt;

&lt;img src='../images/sublogos/suserbanner.gif'&gt;&lt;br&gt;
&lt;?php

if (strlen($loginname2) &lt; 1 || strlen($password) &lt; 3 || strlen($loginname2) &gt; 14 || strlen($firstname) &lt; 3 || strlen($lastname) &lt; 3 || strlen($occupation) &lt; 3 || strlen($email) &lt; 3 || strlen($yearofbirth) &lt; 4 || !($qry3['UserName'])) 
{
?&gt;
&lt;b&gt;Error&lt;/b&gt;&lt;br&gt;Your account could not be created because 1 of these errors below:&lt;br&gt;&lt;/br&gt;
&lt;li&gt;The username you entered may already exist&lt;br&gt;
&lt;li&gt;Your username was not entered&lt;br&gt;
&lt;li&gt;Your username cannot be more than 14 characters&lt;br&gt;
&lt;li&gt;Your password must be a minimum of 3 characters&lt;br&gt;
&lt;li&gt;Your firstname and last name must be more than 3 characters&lt;br&gt;
&lt;li&gt;Your occupation must be more than 3 characters&lt;br&gt;
&lt;li&gt;Your email must be more than 3 characters&lt;br&gt;
&lt;li&gt;Your year of birth must be 4 characters&lt;br&gt;&lt;/br&gt;
&lt;a href='register.php'&gt;Click here&lt;/a&gt; to try again.
&lt;?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!&lt;br&gt;&lt;/br&gt;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");
}
?&gt;
&lt;br&gt;&lt;p&gt;&lt;table border="0" cellspacing="0" cellpadding="0" height="20" id="table1"&gt;
		&lt;tr&gt;
			&lt;td width='87.5' class='btmbtn' align='center'&gt;&lt;b&gt;&lt;/b&gt;&lt;/td&gt;
			&lt;td width='87.5' class='btmbtn' align='center'&gt;&lt;b&gt;&lt;/b&gt;&lt;/td&gt;
			&lt;td width='87.5' class='btmbtn' align='center'&gt;&lt;b&gt;&lt;/b&gt;&lt;/td&gt;
			&lt;td width='87.5' class='btmbtn' align='center'&gt;&lt;b&gt;&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

ok, now you just need to do some basic debugging.

  1. if you add
 
echo $qry3['UserName'];
die();


just above

 
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'])) 


what is the output to the screen?

that outputs

what, nothing?

in that case, either your query didn’t work or it returned zero rows.

you need to investigate which and why.

also, for your if statement to work correctly, all the test conditions must evaluate to either true or false.

sorry i was meant to say it did output the qry properly. How would i add the true and false statements to my code?

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.