rncheckuser.php
To go with rnsignup.php,
here’s Example 20-7, rncheckuser.php, the program that looks up a
username in the database and returns a string indicating whether it has
already been taken. Because it relies on the functions sanitizeString
and queryMysql
, the program first includes the
file rnfunctions.php.
Then, if the $_POST
variable
'user'
has a value, the function
looks it up in the database and, depending on whether it exists as a
username, outputs either “Sorry, already taken” or “Username available.”
Just checking the function mysql_num_rows
against the result is
sufficient for this, as it will return 0 for not found, or 1 if it is
found.
The HTML entity ←
is
also used to preface the string with a little left-pointing
arrow.
<?php // rncheckuser.php include_once 'rnfunctions.php'; if (isset($_POST['user'])) { $user = sanitizeString($_POST['user']); $query = "SELECT * FROM rnmembers WHERE user='$user'"; if (mysql_num_rows(queryMysql($query))) echo "<font color=red> ← Sorry, already taken</font>"; else echo "<font color=green> ← Username available</font>"; } ?>
Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.