JustAngelo
JustAngelo

Reputation: 29

PHP/MYSQL validate

just a simple question. How can I show 1 specific error from this?

$search_query = mysql_query("SELECT * FROM users WHERE username = '$username' OR email = '$email'");
$num_row = mysql_num_rows($search_query);
if($num_row >= 1){
    $errors['username'] = "username is unavailable.";
    $errors['email'] = "Email address is unavailable.";
}else
{//insert.....

They both show the error, but not working with $username.

Just can't figure it out.

And if it's not asking too much, can you please add if the username/email is available?

Thanks in advance.

I'll put the $errors after the input tag:

                    <p>username:</p>
                    <input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>">
                    <?php if(isset($errors['username'])){echo "<h5>" .$errors['username']. "</h5>"; } ?></td>
            </tr>
                <td><p>email:</p>
                    <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>">
                    <?php if(isset($errors['email'])){echo "<h5>" .$errors['email']. "</h5>"; } ?></td>
            </tr>

Upvotes: 0

Views: 41

Answers (1)

M Maavia
M Maavia

Reputation: 340

 mysql_query() or die(mysql_error());

like

$search_query = mysql_query("SELECT * FROM users WHERE username = '$username' OR email = '$email'") or die(mysql_error());

And

  $num_row = mysql_num_rows($search_query)or die(mysql_error());

Upvotes: 3

Related Questions