lingo5 Posted June 20, 2011 Share Posted June 20, 2011 Hi, I`m inserting anew user in the DB like this: $query = "INSERT INTO database.t_usuario (usuario_nombre,usuario,password) ". "VALUES ('$usuario_nombre','$usuario','$password')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_users_display.php"); } what I need to do is to check if the inserted usuario_nombre already exists on the DB and if so echo a message. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/ Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 Change the INSERT INTO into INSERT IGNORE and use mysql_affected_rows... if nothing is inserted, because of a duplicate it will return 0. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232234 Share on other sites More sharing options...
lingo5 Posted June 20, 2011 Author Share Posted June 20, 2011 Thanks HD, but I am still a newbie and don't quite understand all that.. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232236 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 $query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ". "VALUES ('$usuario_nombre','$usuario','$password')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); if(mysql_affected_rows==1) header("Location: PC_users_display.php"); } else{ echo "Duplicate Entry Detected."; } } Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232239 Share on other sites More sharing options...
lingo5 Posted June 20, 2011 Author Share Posted June 20, 2011 Thanks again HD, but page display blank. I have tried removing the last { but then the duplicate found error comes out all the time. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232248 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2011 Share Posted June 20, 2011 Sorry... missed the opening { bracket on the if statement: $query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ". "VALUES ('$usuario_nombre','$usuario','$password')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); if(mysql_affected_rows==1){ header("Location: PC_users_display.php"); } else{ echo "Duplicate Entry Detected."; } Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232250 Share on other sites More sharing options...
lingo5 Posted June 20, 2011 Author Share Posted June 20, 2011 Sorry, but this is still inserting the new user in the DB sfter displaying the duplicate found error....weird Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232255 Share on other sites More sharing options...
lingo5 Posted June 20, 2011 Author Share Posted June 20, 2011 I've changed my mind and I want to insert users in a different table. This is how I insert a nw user at the moment: $query = "INSERT INTO database.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ". "VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_clientes_display.php"); } I want to check if '$cliente_email' (client's email address) already exists before inserting the new user. If so, print an error message. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232272 Share on other sites More sharing options...
lingo5 Posted June 20, 2011 Author Share Posted June 20, 2011 Please anyone, is there an easy method to check for a duplicate entry? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232295 Share on other sites More sharing options...
ebmigue Posted June 21, 2011 Share Posted June 21, 2011 I assume you are using MySQL. Create a UNIQUE INDEX on the field that you do not want duplicated. (see MySQL manual). Wrap your code that actually inserts in a TRY{...}CATCH{...} block. If it enters the CATCH block, examine the error message, and perform further processing. Or just notify the user of the error message. Alternatively, before inserting, perform a SELECT * FROM t_clientes where cliente_email= '$cliente_email' It that query returned rows (i.e., row >= 1) throw a "email already used" exception. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232575 Share on other sites More sharing options...
lingo5 Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks a lot ebmigue, this is how i've finally done it and it works: mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_checkdup_RS = sprintf("SELECT * FROM t_clientes WHERE cliente_email = %s", GetSQLValueString($colname_checkdup_RS, "text")); $checkdup_RS = mysql_query($query_checkdup_RS, $MySQLconnect) or die(mysql_error()); $row_checkdup_RS = mysql_fetch_assoc($checkdup_RS); $totalRows_checkdup_RS = mysql_num_rows($checkdup_RS); if($totalRows_checkdup_RS==1) { echo "Duplicate Entry Detected."; } else{ $query = "INSERT INTO databse.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ". "VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_clientes_display.php"); } } Now I want to print the error message within the html code instead of inside the head section. I have tried this $duplicate = "<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="CP_SiNoText">la direccón de email ya existe en la base de datos.</td> </tr> </table>"; and then rinting it inside the html section like this <?php echo $duplicate;?> but nothing happens. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232714 Share on other sites More sharing options...
Far Cry Posted June 21, 2011 Share Posted June 21, 2011 You could also use a Primary key. Quote Link to comment https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/#findComment-1232871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.