PHP & MySQL username validation and storage problem.
Posted
by php
on Stack Overflow
See other posts from Stack Overflow
or by php
Published on 2010-05-02T08:50:15Z
Indexed on
2010/05/02
8:57 UTC
Read the original article
Hit count: 157
For some reason when a user enters a brand new username the error message <p>Username unavailable</p>
is displayed and the name is not stored. I was wondering if some can help find the flaw in my code so I can fix this error? Thanks
Here is the PHP code.
if($_POST['username'] && trim($_POST['username'])!=='') {
$u = "SELECT *
FROM users
WHERE username = '$username'
AND user_id <> '$user_id'";
$r = mysqli_query ($mysqli, $u) or trigger_error("Query: $u\n<br />MySQL Error: " . mysqli_error($mysqli));
if (mysqli_num_rows($r) == TRUE) {
echo '<p>Username unavailable</p>';
$_POST['username'] = NULL;
} else if(isset($_POST['username']) && mysqli_num_rows($r) == 0 && strlen($_POST['username']) <= 255) {
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
} else if($_POST['username'] && strlen($_POST['username']) >= 256) {
echo '<p>Username can not exceed 255 characters</p>';
}
}
© Stack Overflow or respective owner