Jump to content

Codeigniter Insert Problem


smithdp1

Recommended Posts

I am pretty new to codeigniter and need help. I have a view that list 3 names with a check box to each name. What i am trying to do is post the items that are checked off to a database.(clientid, camapignid values of only selected).

 

Here are the tables in my database.

id - auto increment

clientId

campaignId

creationdate

 

Here is the code for my view:

<html>
<head>
<title>Client View</title>
<style type="text/css">

label{
display: block;
}
</style>
</head>
<body>
<h2>Insert</h2>
<?php echo form_open('site/create_client'); ?>
<p>
<input type="checkbox" name="clientid[]" value="10"> - Fred
<input type="checkbox" name="clientid[]" value="20"> - Bob
<input type="checkbox" name="clientid[]" value="30"> - Mary
<input type="hidden" name="campaignid[]" value="2">
</p>
<p>
<input type="submit" value="Submit"/>
</p>
<?php echo form_close(); ?>
</body>
</html>

 

Here is the code for my controller:

<?php
class Client extends CI_Controller{

function index(){

$this->load->view('client');
}


function create(){
$datetime = date('Y-m-d H:i:s');
$data = array(
'clientid'=> $this->input->post('clientid'),
'campaignid'=> $this->input->post('campaignid'),
'creationdate'=> $datetime
);

$this->client->add_record($data);
$this->index();

}
}

 

Here is the code for my model:

<?php
class client extends CI_Model{

function add_record($data){
$this->db->insert('campaign_to_client', $data);
return;
}
}

 

I know I am missing some kind of loop somewhere but cant figure it out.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/272202-codeigniter-insert-problem/
Share on other sites

You explained what you want to accomplish, but I do not see any problem description or debugging attempts.

 

- Using brackets means passing an array to the controller, so you need to do a corresponding action there to meet the array's needs.

- you need to SET the date in your model: $this->db->set('creationdate', 'now()', false);

- If you want to insert a row for each checkbox ticked your database model needs to be more complex.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.