I have registration form, which is saving user info in ‘tblusers’ -> ‘Username’, but It doesn’t check if the user is already existing, so at the moment users can register with identical usernames, which is a huge mess.
So I need to check MySQL database for existing ‘Username’ on the registration. Any idea how to do this with PHP – MySQL, even javascript.
This is code I’m executing on form submit:
check_token ();
$_SESSION['currency'] = $currency;
$clientid = addclient ($firstname, $lastname, $companyname, $email, $address1, $address2, $city, $state,
$postcode, $country, $phonenumber, $password, $securityqid, $securityqans, $sendemail);
//print "New clientid=" . $clientid . "<br>";
update_query ('tblclients', array ('notes' => $notes, 'status' => $status, 'credit' => $credit, 'salesOperatorId' => $salesOperatorId,
'taxexempt' => $taxexempt, 'latefeeoveride' => $latefeeoveride, 'overideduenotices' => $overideduenotices, 'country' => 'US',
'language' => $language, 'billingcid' => $billingcid, 'lastlogin' => '00000000000000'), array ('id' => $clientid));
//print "New clientid now=" . $clientid . "<br>";
savecustomfields ($clientid, $customfield);
$result = select_query ('tblusers', 'ID', array ('ClientID' => $clientid));
// adding user to the DB at same time as client is being added ...
$datenowtimestamp = date("Y-m-d H:i:s");
$table91 = 'tblusers';
$array91 = array ('ClientID' => $clientid, 'firstname' => $userfirstname, 'lastname' => $userlastname, 'email' => $useremail,
'username' => $username, 'password' => $password, 'searchdelivery' => 'Criminal', 'reporttiming' => 'Instant',
'deliverymethod' => 'email', 'Created' => $datenowtimestamp,
'phonenumber' => $phonenumber, 'orderreports' => 1, 'viewallreports' => 1, 'viewpricing' => 1,
'restrictreportview' => 0, 'gatewayaccess' => 0);
insert_query ($table91, $array91);
if($data = mysql_fetch_array ($result))
$userid = $data['ID'];
$header = 'Location: clientsusers.php?clientid=' . $clientid;
//print $header . "<br>";
header ($header);
exit ();
The right way to do this is probably by adding unique indexes on your
usernamecolumn in your users table:The alternative (checking in your PHP code) is liable to issues with two people trying to register with the same username at the same time.