Search code examples
mysqlcodeignitertransactionscsrf-protection

insert into two tables at once using codeigniter & innodb engine mysql


Hello I have two tables one is users & other is parent

mysql query for parent is

CREATE TABLE  `parent` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL DEFAULT '0',
  `name` longtext NOT NULL,
  `dob` int(10) unsigned NOT NULL DEFAULT '0',
  `sex` longtext NOT NULL,
  `address` longtext NOT NULL,
  `contact` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

mysql query for user is

CREATE TABLE  `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` longtext NOT NULL,
  `username` longtext NOT NULL,
  `password` longtext NOT NULL,
  `type` longtext NOT NULL,
  `status` int(10) unsigned NOT NULL DEFAULT '0',
  `online` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;

I have a form which contain all above details. Now i want to insert them in their respective table. I am also using CSRF Protection

What I have tried

MY Controller Code

public function create()
{
$udata=array(
        'email'=>$this->input->post('email'),
        'password'=>md5($this->input->post('password')),
        'type'=>'parent',
        'online'=>'0',
        'status'=>'1'
        );
        $pdata=array(
        'name'=>$this->input->post('name'),
        'dob'=>strtotime($this->input->post('dob')),
        'gender'=>$this->input->post('gender'),
        'address'=>$this->input->post('address'),
        'contact'=>$this->input->post('contact')
        );
        $pid= $this->user_model->insert($udata,$pdata);  
}

My MODEL Data

function insert($userdata,$typedata) 
    {
    $this->db->insert('users', $userdata);
    $typedata['user_id'] = $this->db->insert_id();
    $this->db->insert('parent', $typedata);
    return $this->db->insert_id(); 
    }

the above code only insert values to first table i.e users, and other table i.e parent is empty.

Please Note : user_id field of parent table is the primary key (auto increment) of users table


Solution

  • Problem Solved by using error Logging Thanks @joerg