Sleeper Posted February 17, 2011 Share Posted February 17, 2011 I have set an SQL field as unique so that duplicates can't be entered, however rather then having the page error out and show the Error: Duplicate entry 'entry' for key 'field' I would like to be able to have a little div appear and say something like "this entry already exists" I already have it set to show that the item has been added but I have no idea were to start to make this work. Any ideas? Thanks, Jim Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/ Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2011 Share Posted February 17, 2011 The error number for a duplicate entry should be 1062. You can use mysql_errno with a conditional to check for a duplicate entry error. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175668 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 I know im a nube but can you explain that a bit more on how I would do that? Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175671 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 alternatively, don't let the error happen. check to see if there is a duplicate and tell the user if there is one. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175672 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 That would be exactly what I want to do, however I don't know how to go about doing that. That's the reason for this post. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175674 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 I would use a SQL select statement to check if there is already a record with the matching value. if the number of results returned == 0, then there is no existing match and everything is okay. if the number of results > 0, there is a match and the user should get the warning. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175677 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 Again your telling me what I want to do, but not really giving me an answer of how to do that..lol. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175678 Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2011 Share Posted February 17, 2011 That works also. I usually check for the error just to minimize the number of queries that get executed. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175679 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 Again your telling me what I want to do, but not really giving me an answer of how to do that..lol. I assume that you know how to run a SQL query in PHP, as the question pertains to a problem you see when you do that. So which part do you have questions on, the PHP or the SQL syntax? Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175680 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 To keep things moving at a good speed I think it would be better to let it give the error rather then checking every field in the table. I know I can do INSERT IGNORE INTO and it will ignore the error and just do nothing. But then it will come back like its been inserted as far as the user would know. What I want to do is using some type of coding that will check to see if there is an error when inserting and then if there is to display the text saying the option already exists. Is that possible and if so how? Of that is not possible, what code would I use to have it check the existing fields to see if a duplicate exists? Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175683 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 To check to see if there was an error: The error number for a duplicate entry should be 1062. You can use mysql_errno with a conditional to check for a duplicate entry error. To check existing fields: I would use a SQL select statement to check if there is already a record with the matching value. if the number of results returned == 0, then there is no existing match and everything is okay. if the number of results > 0, there is a match and the user should get the warning. example: $inputval = 'something'; // Does this value exist in field somefield in table sometable? $sql = "SELECT id FROM sometable WHERE somefield = '".mysql_real_escape_string($inputval)."'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_numrows($result) > 0) { // This value already exists, so show error } else { // This value does not already exist } Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175686 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 Ok I put that into effect but I'm still getting the error rather then the wanted effect. Here is the code I have maybe I'm missing something. if ($add == yes){ $sql = "SELECT id FROM $GT WHERE link = '".mysql_real_escape_string(htmlentities($_POST[link]))."'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_numrows($result) > 0) { echo "<div id='success'><table border='0' cellpadding='0' cellspacing='0' width='450' class='success'><tr> <td valign='top' align='center'>ERROR: This Link Already Exists!</td></tr></table></div> <script type='text/javascript'> window.setTimeout('closeHelpDiv();', 5000); function closeHelpDiv(){ document.getElementById('success').style.display='none';}</script>"; }else { $data="INSERT INTO $GT (link) VALUES ('".mysql_real_escape_string(htmlentities($_POST[link]))."')"; if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); echo "<div id='success'><table border='0' cellpadding='0' cellspacing='0' width='450' class='success'><tr> <td valign='top' align='center'>New Link Added Successfully!</td></tr></table></div> <script type='text/javascript'> window.setTimeout('closeHelpDiv();', 5000); function closeHelpDiv(){ document.getElementById('success').style.display='none';}</script>"; }} Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175693 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 die('Error: ' . mysql_error()); this causes the code to stop and display mysql_error(). you should remove that line and get mysql_errno() to check for error number 1062, which is duplicate key Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175698 Share on other sites More sharing options...
Sleeper Posted February 17, 2011 Author Share Posted February 17, 2011 that worked. Ty. Link to comment https://forums.phpfreaks.com/topic/227996-error-handling/#findComment-1175708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.