0

I want to insert data in codeigniter.

Here is my view:

<input type="text" placeholder="Name" name="name[]">
<input type="text" placeholder="Work Name" name="work[]">

Here is my Model

public function add_work(){
$names = $_POST['name'];
$works = $_POST['work'];

foreach($names as $key => $name){
            $work = $works[$key];

            $insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
   }
}

Codeigniter refuse to use mysql query. How to fix that. Thanks in advance

9
  • Do you have more than one textboxes with name="name[]" and name="work[]"?? Commented Oct 17, 2015 at 5:32
  • Yes. I have more than one textboxes Commented Oct 17, 2015 at 5:33
  • Hey you need to connect mysql first if you use it like this. Commented Oct 17, 2015 at 5:36
  • Please add code here. Commented Oct 17, 2015 at 5:37
  • 1
    Try this if you want to query in codeigniter $this->db->query('YOUR QUERY HERE'); for the reference check out this: ellislab.com/codeigniter/user-guide/database/queries.html Commented Oct 17, 2015 at 5:41

1 Answer 1

3

in your controller

class CommonController extends CI_Controller {
  public function __construct() {
    parent::__construct();
      $this->load->model('common_model'); //load your model my model is "common model"
  }

 public function add_work(){
 $names = $_POST['name'];
 $works = $_POST['work'];

 foreach($names as $key => $name){
             $name= "your specified name";
             $insertdata = array();
             $insertdata['work'] = $works[$key];
             $insertdata['name'] = $name;
             $this->common_model->insert($insertdata);
        //$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
            }
  }
}

and in your model "common_model.php"

 class Common_model extends CI_Model {

/**
 * Constructor 
 *
 */
   public function __construct()
 {
     parent::__construct();

  }//Controller End
  public function insert($insertData=array()){
     $this->db->insert('work', $insertData);
  }


}
Sign up to request clarification or add additional context in comments.

4 Comments

Getting Undefined variable: insertData
now getting You must use the "set" method to update an entry.
do you have any prefixes to the table name?
ok try this inside insert function in model. $this->db->set($insertData); $this->db->insert('work');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.