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!

Edited by smithdp1
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.