PHP / MYSQL Retry wehn duplicate happens -


$new_acct_id = 'acc'.strtoupper( substr(md5(uniqid(rand())),0,5) );   mysql_query("insert `accounts` (id, market_unit_id, account_type_id, is_inactive,inactive_notes,is_special,dealer_id, is_premium) values ('".$new_acct_id."', '".$mu_id."', '".$atype['0']."', '".$is_inactive."','','','', '".$is_premium."')") or die("load2 -" . mysql_error()); 

load2 -duplicate entry 'acc96e38' key 'primary'

is there anyway can have generate number if first creates error?

this should it.

do{     $error='';     $new_acct_id = 'acc'.strtoupper( substr(md5(uniqid(rand())),0,5) );     $res=mysql_query("insert `accounts` (id, market_unit_id, account_type_id, is_inactive,inactive_notes,is_special,dealer_id, is_premium)     values ('".$new_acct_id."', '".$mu_id."', '".$atype['0']."', '".$is_inactive."','','','', '".$is_premium."')");     if(!$res)$error=mysql_error(); }while(!$res && strpos($error,'duplicate entry')!==false); if(!$res)die($error); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -