Welcome Guest, Not a member yet? Register   Sign In
Error Number: 1062 HELP
#1

[eluser]maniking[/eluser]
Hello Guyz Need Help

This is error when i try to add duplicate country names.

Error Number: 1062

Duplicate entry 'italy ' for key 'Country_Name_UNIQUE'

INSERT INTO `country` (`fkContinent_id`, `Country_Name`, `Country_ShortName`, `Country_Url`, `Country_PageTitle`, `Country_MetaKeywords`, `Country_MetaDescription`, `Country_PageContent`, `Country_Ext`) VALUES ('11', 'italy ', '', 'italy-', '', '', '', '', '')

Filename: C:\xampp\htdocs\score\system\database\DB_driver.php

Line Number: 330


My Controller :

Code:
function add()
{
  authenticate();
  
  if(submitted())
  {
   $arr = explode(".", $_FILES['picture']['name']);
   $_POST['Country_Ext'] = $ext = $arr[count($arr)-1];
    $this->db->insert("country", $_POST);
   $path = "images/country/{$_POST['Country_Url']}.{$ext}";
   move_uploaded_file($_FILES['picture']['tmp_name'], $path);
   redirect("admins/country/index/added");
  }
  
  $data['content'] = $this->load->view("admin/country/add", array(), true);
  $this->load->view("admin/template", $data);
}

i wanted to print this error msg on my page to avoid this sql error on page i m new please help

Thanks
#2

[eluser]CroNiX[/eluser]
Turn off db_debug in database config and use
Code:
$this->db->_error_message();
$this->db->_error_number();
Although you will no longer see error messages for any query with db_debug turned off and have to handle them all manually, which isn't a good idea. It would be much better to query to see if that value exists first and return error based on that, or insert if it doesn't.

You are also doing something very dangerous by inserting $_POST directly with no data validation. Very insecure.

All of CI's db "write" type statements (insert/update/etc) return a boolean FALSE if there is an error.

Code:
if ($this->db->insert("country", $_POST) === FALSE)
{
  //there was an error on insert, do something
}
#3

[eluser]maniking[/eluser]
Thanks for replay I will try now this code...

And how I can make secure that $_POST data validation?

Thanks
#4

[eluser]maniking[/eluser]
I tryed this code but still same error coming..

My Controller :
Code:
function add()
{
  authenticate();
  
  if(submitted())
  {
   $arr = explode(".", $_FILES['picture']['name']);
   $_POST['Country_Ext'] = $ext = $arr[count($arr)-1];
    $this->db->insert("country", $_POST);
    if($this->db->insert("country", $_POST)== FALSE)
    {
    echo 'Dublicate Entry Error';
    }
   $path = "images/country/{$_POST['Country_Url']}.{$ext}";
   move_uploaded_file($_FILES['picture']['tmp_name'], $path);
   redirect("admins/country/index/added");
  }
  
  $data['content'] = $this->load->view("admin/country/add", array(), true);
  $this->load->view("admin/template", $data);
}


My View Page Where i Enter Data :

Code:
<form method="post" enctype="multipart/form-data">
Select Continent<br />
<select name="fkContinent_id">
  &lt;?php
  $continents = $this->db->get("continent");
  
  foreach($continents->result() as $category)
  {
   echo "<option value='{$category-&gt;Continent_id}'>{$category->Continent_Name}</option>";
  }
  ?&gt;
</select>
<br /><br />
Country Name<br />
&lt;input name="Country_Name" id="title" /&gt;

<br /><br />
Country Short Name<br />
&lt;input name="Country_ShortName" id="title" /&gt;

<br /><br />

URL<br />
&lt;input name="Country_Url" id="url" /&gt;

<br /><br />
Image<br />
&lt;input name='picture' type="file" /&gt;

<br /><br />
Page Title<br />
&lt;textarea name="Country_PageTitle"&gt;&lt;/textarea>

<br /><br />
Meta Keywords<br />
&lt;textarea name="Country_MetaKeywords"&gt;&lt;/textarea>

<br /><br />
Meta Description<br />
&lt;textarea name="Country_MetaDescription"&gt;&lt;/textarea>

<br /><br />
Article<br />
&lt;textarea name="Country_PageContent"&gt;&lt;/textarea>

<br /><br />
&lt;input type="submit" value="Save" /&gt;
&lt;/form&gt;


Thanks
#5

[eluser]CroNiX[/eluser]
Did you turn db_debug off? db_debug captures and displays all db errors automatically, so you need to turn it off if you want to handle them manually.

For validation of form data: http://ellislab.com/codeigniter/user-gui...ation.html

You are running the same query 2x now.

It would be much better to check if the value exists first in the db before trying to insert a new one, rather than relying on errors.




Theme © iAndrew 2016 - Forum software by © MyBB