I am learning something new with checking for whether a username is used in the database and have created some code based on a tutorial I found online. I understand the logic, though not sure I have approached it the right way. In essence. Information is passed over from a form field. If what has been entered matches a field in the database then I want it to return / echo a result 'yes'. if it does not match, I need it to echo 'no'. Seems straight forward.
The tutorial is designed for predetermined values. i.e. $existing_users=array('test',''one','two',three');
Though I want the 'test',''one','two',three' to actually pull dynamically from a database.
So I went about adding the setting it up to do so, though the code I have written doesn't work when I try to place dynamic values in. My code is as below:
$existing_users = array();
mysql_select_db($database_db, $db);
$result = mysql_query('SELECT * FROM clients') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$existing_users[] = $row['shortcode'];}
$arr = $existing_users;
$display = "'" . implode("', '", $arr) . "'";
// THIS IS THE PROBLEM
// If the code is written out as:
// $existing_users=array('test',''one','two',three');
// It works.
// When the script is coded as below. It doesn't work.
// Note. If I echo $display is displays 'test',''one','two',three'
$existing_users=array($display);
//value received from the get method
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
//user name is not available
echo "no";
}
else
{
//user name is available
echo "yes";
}
I am not sure whether I am approaching this the right way because I am hacking an online tute, there potentially could be a far easier way. Any thoughts would be greatly appreciated.