Jump to content

Error Handling


Sleeper

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.