CodeIgniter Forums
duplicate entry error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: duplicate entry error (/showthread.php?tid=14058)



duplicate entry error - El Forum - 12-16-2008

[eluser]crumpet[/eluser]
Is there a way to display your own duplicate entry error without having to do an extra query to check if the insert you are making is duplicate?

Instead of the user seeing this

An Error Was Encountered

Error Number: 1062

Duplicate entry '1-100110103401' for key 1

I want them to see something like, "You already added that"

Do i need to do a query before my insert statement to check if the insert is possible?


duplicate entry error - El Forum - 12-16-2008

[eluser]Armchair Samurai[/eluser]
If you're only using MySQL and portability is not an issue, I'd look into INSERT IGNORE and INSERT ON DUPLICATE KEY UPDATE.

For example, you could do something like this:
Code:
$sql = "INSERT IGNORE INTO this_table (id, data)
        VALUES (?, ?)";

$this->db->query($sql, array($foo, $bar));

return $this->db->affected_rows() == 1 ? TRUE : FALSE;



duplicate entry error - El Forum - 01-09-2009

[eluser]learnq[/eluser]
I thing its late reply ..but if some one want to show user friendly error message
trun off the debuging

in config/database.php

$db['default']['db_debug'] = FALSE;

and than .. as said ...like

...
return $this->db->affected_rows() == 1 ? TRUE : FALSE;

Hope it help others.


duplicate entry error - El Forum - 01-09-2009

[eluser]Phil Sturgeon[/eluser]
That or a Validation callback function.


duplicate entry error - El Forum - 04-23-2012

[eluser]Unknown[/eluser]
I have the same problem, and then I try this

this is the view
Code:
<form action="insert_data" method="post">
Email : <input type="text" name="email">
<input type="submit" value="Submit">
</form>

this is the controller code
Code:
function insert_data()
{
//get the data from database to check whether the data that you've inserted is already exists or not
$check = $this->your_model->check_data();
   if($check > 0) //if the data exists show error message
   {
//write the error message
   }
   else //if the data is not exists, save the data
   {
    $this->your_model->save_data();
   }
}

and this is the model
Code:
//the function to check whether the data inserted by user is already exists or not
function check_data()
{
  $this->db->where('email', $this->input->post('email'));
  $query = $this->db->get('table');
  return $query->num_rows();
}

with this, you don't have to set the database config
Code:
$db['default']['db_debug'] = TRUE
to
Code:
$db['default']['db_debug'] = FALSE

#sorry if my english is bad, I hope this will help Smile +