benoit1980 Posted June 2, 2013 Share Posted June 2, 2013 (edited) Hello all. I have only started php a week ago and require your help as this problem is driving me crazy...... I have a form and the customers just enter their phone and email. When a duplicate of any of them is found I wish to show the message "Your phone or Mobile is already registered" Now when no duplication is found, I wish to echo something else. I have done this code below but I cannot get it to work....I created the database, everything is going well in without problem when I remove the statement <?php if(mysql_affected_rows() == 0){ echo '<div align="center" class="text-not-correct">Your Phone Number or Email or both are already registered!</div>';?> Here is part of my code, I only need in this tiny area please. <?php$link = mysqli_connect("localhost", $username, $password, $db_name)or die("cannot connect"); $mres_email = mysqli_real_escape_string($link, $_POST['email_form2']);$mres_phone = mysqli_real_escape_string($link, $_POST['phone_form2']);$mres_code = mysqli_real_escape_string($link, $_POST['code_form2']); $query = "INSERT INTO $tbl_name VALUES (NULL, '$dat_date', '$mres_phone', '$mres_email', '$mres_code')";mysqli_query($link, $query);if(mysql_affected_rows() == 0){ echo '<div align="center" class="text-not-correct">Your Phone Number or Email or both are already registered!</div>'; mysqli_close($link); }else{header("Refresh: 5;url=http://pwebsite.com/vio/page1.php");echo '<div align="center" class="text-correct">Congratulation you are now registered!</div></br>';echo '<div align="center"><img src="loading.gif" width="100" height="100" /></div>'; ?> Remember, the script is working fine but not when I try to check for duplicate. please help :-)))) Thank you! Benoit Edited June 2, 2013 by benoit1980 Quote Link to comment https://forums.phpfreaks.com/topic/278693-return-error-in-php-when-mysql-duplicate-entry-is-found/ Share on other sites More sharing options...
jcbones Posted June 2, 2013 Share Posted June 2, 2013 I would check the mysqli_errno to see if it is returning a duplicate key error. Not sure of number for that specific error, but you can find it in the mysqld_error.txt file in the docs folder of your installation. Quote Link to comment https://forums.phpfreaks.com/topic/278693-return-error-in-php-when-mysql-duplicate-entry-is-found/#findComment-1433704 Share on other sites More sharing options...
benoit1980 Posted June 3, 2013 Author Share Posted June 3, 2013 Thanks jcbones. I actually used this: if(mysqli_affected_rows($link) <= 0){ echo 'something here...'; It works very well :-))0 Hope this help someone. Bye all and thank you for your help. Ben Quote Link to comment https://forums.phpfreaks.com/topic/278693-return-error-in-php-when-mysql-duplicate-entry-is-found/#findComment-1433787 Share on other sites More sharing options...
litebearer Posted June 3, 2013 Share Posted June 3, 2013 Also look into unique constraints for your table fields - http://dev.mysql.com/doc/refman/5.0/en/create-table.html Quote Link to comment https://forums.phpfreaks.com/topic/278693-return-error-in-php-when-mysql-duplicate-entry-is-found/#findComment-1433796 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.