• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to catch db error

#1
i have insert new row to table in same time  ( 10 users )

my primary key is determined from user input with in 3 groups

Blue
Yellow
Green

if user select Blue ID must be B001, B002
Yellow Y001, Y002


i have check to get last Id using query but sometimes getting error duplicate key
i want to catch db error so i can change ID and try to insert again

    try{
      $result = $this->db->insert($table,$data);
      echo $result;
      print  $result;
    }
    catch(Exception $e)
    {

       echo "Test Error"
        //get new ID

        // Insert data with new ID
    }

but now working
Reply

#2
PHP Code:
$this->db->error(); 

CodeIgniter User Guide - Handling Errors
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Try

PHP Code:
if($this->db->insert($table,$data)){
  return true;
}else{
  return false;
    
//or
  
echo $this->db->error();

Reply

#4
not working
in controller :

$check=false;
while (!$check)
{
         $no = $this->m_home->getLastId($grp);
         $data = array(
            'no' => $no,
            'usergroup' => $group,
            'name' => $n,
            'mobile' => $m
            );
       
         $check= $this->m_home->input_data($data,'users');
}


in model

function input_data($data,$table)
  {
    $this->db->db_debug = false;
  
    if($this->db->insert($table,$data))
    {
        return true;
    }
    else
    {
      return false;
    }
  }
Reply

#5
A better way would be to create a separate table for holding the last id's.

table last_id
blue_id
yellow_id
green_id

When you create the users record assign the correct id with an increment method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(3 hours ago)InsiteFX Wrote: A better way would be to create a separate table for holding the last id's.

table last_id
blue_id
yellow_id
green_id

When you create the users record assign the correct id with an increment method.

still cannot, because data entry by many people / user

it could be red inputted simultaneously by several users
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.