Hey

Im having this problem, im sure theres a simple solution, but i cant seem to see it :D

The code bellow is part of a regestration form validation. Basicly when i ask the database if anyone else exists with the same username, password or email address - it works

BUT

Only if it returns 1 or more rows!
If no-one else exists with the username ect. it returns my error message (couldnt check rows).

<form  method="post" name="details">

<b class="form_title">First Name: 
</b><input type="text" name="first_name"  class="form_box" />
<br />

<b class="form_title">Last Name: 
</b><input type="text" name="last_name"  class="form_box"/>
<br />

<b class="form_title">Age: 
</b><input type="text" name="age"  class="form_box" />
<br />

<b class="form_title" >Email: 
</b><input type="text" name="email"   class="form_box" />
<br />

<b class="form_title" >Phone: 
</b><input type="text" name="phone"  class="form_box" />
<br />

<b class="form_title">Address: 
</b><textarea name="address"  class="form_box"></textarea>
<br />

<b class="form_title" >Username: 
</b><input type="text" name="username" class="form_box"/>
<br />

<b class="form_title" >Password:
</b><input type="password" name="password1" class="password"/>
<br />

<b class="form_title" >Repeat: 
</b><input type="password" name="password2"  class="password" />
<br />

<b class="form_title">I accept the terms of using this website
</b> <input type="checkbox" name="terms"  class="form_box" />
<br />

<input type="submit" value="Regester Me"/>

</form>


<?php
$priv = "LIMITED";
$first_name = $_REQUEST["first_name"];
$last_name = $_REQUEST["last_name"];
$age = $_REQUEST["age"];
$email = $_REQUEST["email"];
$phone = $_REQUEST["phone"];
$address = $_REQUEST["address"];
$username = $_REQUEST["username"];
$pass1 = $_REQUEST["password1"];
$pass2 = $_REQUEST["password2"];
$terms = $_REQUEST["terms"];



    if (empty($first_name) || empty($last_name) || empty($age) ||
    empty($email) || empty($phone) || empty($address) || empty($username) || empty($pass1) || empty($pass2)) {
    $error = "You Must Fill In All The Boxs.";
    }

    elseif ($pass1 != $pass2) {
    $error = "Your Passwords Must Match.";
    }
    elseif ($terms != "on") {
    $error = "You Must Accept The Terms";
    }

    else {

    $check_email = "SELECT email,username,password FROM members WHERE 
                   email = '$email' OR
                   username = '$username' OR
				   password = '$pass1'";
                   $check_email_sql = MYSQL_QUERY($check_email) or die ("Could send email check");
                   $email_rows = MYSQL_NUMROWS($check_email_sql) or die ("couldnt check rows");          

   }
   echo $email_rows;
 

?>
<br />
<u class="error"><?php echo $error ; ?></u>
<div id="space">
</div>
</div>

All help would be greatly appreciated!
Thanks

It may be producing an error because there are no rows.

You might be better to write the script as:

$email_rows = mysql_num_rows($check_email_sql);

if($email_rows>0){
echo $email_rows;
}else if($email_rows==0){
echo 'No Rows Found';
}

Wow
Worked a treat thankyou!

Can i ask why though?
The scripts seem extreamly similar :D

Thanks again

I think it's because when the row return's zero then php thinks it's an error so it runs the die() function.

I always write my scripts like the one I showed you and I never have a problem.

Please use "Add to Phper's reputation" if you found my post usefull!

Thanks

commented: Fantastic advice, thanks a bunch! +1
Member Avatar for amigura

why would you want to check for password? not needed!

btw $email_rows = MYSQL_NUMROWS($check_email_sql); should be $email_rows = MYSQL_NUMROWS($check_email)

$email_rows =mysql_affected_rows();
if($email_rows>0){
echo $email_rows;
}else if($email_rows==0){
echo 'No Rows Found';
}

you need to use mysql_real_escape_string($username) on db also

username='".mysql_real_escape_string($username)."'

I believe it's mysql_num_rows(), in addition, in case you wanna switch to mysqli result object, it's $result -> num_rows;

Member Avatar for amigura

ah yes yang, few typos there.

$email_rows = MYSQL_NUMROWS($check_email_sql); should be $email_rows = MYSQL_NUM_ROWS($check_email_sql)

Lol
Thanks all
Il mark them up now, oh and i do need to check against current email address in my database because each member is limited to one account, if you get what i meen.

Anyways thanks again, phper in particular.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.