Reputation: 341
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 ();
Upvotes: 0
Views: 675
Reputation: 4946
When the form data is accessible in whatever php script gets executed when the form is submitted in a has $_POST['username']
(or if you're using GET, $_GET['username']
). From there, since you're users can register, I'll assume you connect to your DB and run queries.
Query the database for how many entries match $_POST['username']. If if greater than 0, return the error however you see fit.
The query would look something like SELECT count(*) FROM dbname WHERE username = $_POST['username']
. Of course there is some prep work to do to avoid SQL injection, but that would be the basic process.
This book has everything you need to know about the basics of web programming, and was the text for my web programming class.
Upvotes: 1
Reputation: 2113
when user enters username while registering, make an ajax check to see if the username already exists and notify the user to choose something else
Upvotes: 1
Reputation: 99841
The right way to do this is probably by adding unique indexes on your username
column in your users table:
CREATE UNIQUE INDEX username_index ON users (username);
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.
Upvotes: 4