I have a database called Customers with Unique ID, so each time insert from php to database I need to generate a new ID for the new customer from a random function but I need to check the id that generated is exists in database or not.
This is the php random function:
function randomDigits($length){
$digits = "";
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
I tried to pass random length like this:
$cusid = randomDigits(10);
$generate_customer_id = "";
$query_customer_id = dbQuery("SELECT customer_id FROM customers WHERE customer_id = '$cusid'");
$count_customer_id = $query_customer_id->rowCount();
if($count_customer_id == 1)
{
$generate_customer_id = randomDigits(10);
}
else
{
$generate_customer_id = $cusid;
}
$query = dbQuery("INSERT INTO customers(`customer_id`,`username`,`password`,`email`,`first_name`,`last_name`,`dob`,`id_card`,`family_book_id`,`address`,`district`,`province`,`phone`,`mobile`,`father_name`,`mother_name`,`bank_account_no`,`bank_account_name`) VALUES('$generate_customer_id','$username','$password','$email','$firstname','$lastname','$dob','$idcard','$familybook','$address','$district','$province','$homephone','$mobilephone','$fathername','$mothername','$bankno','$bankname')");
if($query == true)
{
$insert_referrer = dbQuery("INSERT INTO referrers (`parent_id`, `customer_id`, `category_id`, `created`,`modified`) VALUES(NULL, '$generate_customer_id', 2, NOW(), NOW())");
if($insert_referrer == true)
{
echo "OK";
}
else
{
echo "There is an error please check your information and try again referrers!";
}
}
else
{
echo "There is an error please check your information and try again customers!";
}
But data return from my AJAX function, if return error I need to click the save customer button many times then i will return success and then data successful saved to database.
But I want user to click only one time then data will insert.
How to implement this please help!
Aucun commentaire:
Enregistrer un commentaire