ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Error Number: 1062 HELP

May 22, 2014 7:02pm

Subscribe [2]
  • #1 / May 22, 2014 7:02pm

    maniking

    2 posts

    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 :

    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 / May 23, 2014 10:40am

    CroNiX

    4713 posts

    Turn off db_debug in database config and use

    $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.

    if ($this->db->insert("country", $_POST) === FALSE)
    {
      //there was an error on insert, do something
    }
  • #3 / May 23, 2014 11:14am

    maniking

    2 posts

    Thanks for replay I will try now this code…

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

    Thanks

  • #4 / May 23, 2014 11:21am

    maniking

    2 posts

    I tryed this code but still same error coming..

    My Controller :

    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 :

    <form method="post" enctype="multipart/form-data">
     Select Continent
    
     <select name="fkContinent_id">
      <?php
      $continents = $this->db->get("continent");
      
      foreach($continents->result() as $category)
      { 
       echo "<option value='{$category->Continent_id}'>{$category->Continent_Name}</option>";
      }
      ?>
     </select>
     
    
    
     Country Name
    
     <input name="Country_Name" id="title" />
    
     
    
    
     Country Short Name
    
     <input name="Country_ShortName" id="title" />
    
     
    
    
    
     URL
    
     <input name="Country_Url" id="url" />
     
     
    
    
     Image
    
     <input name='picture' type="file" />
     
     
    
    
     Page Title
    
     <textarea name="Country_PageTitle"></textarea>
     
     
    
    
     Meta Keywords
    
     <textarea name="Country_MetaKeywords"></textarea>
     
     
    
    
     Meta Description
    
     <textarea name="Country_MetaDescription"></textarea>
     
     
    
    
     Article
    
     <textarea name="Country_PageContent"></textarea>
     
     
    
    
     <input type="submit" value="Save" />
    </form>


    Thanks

  • #5 / May 23, 2014 11:30am

    CroNiX

    4713 posts

    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-guide/libraries/form_validation.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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases