Welcome Guest, Not a member yet? Register   Sign In
Codeigniter - need help with insert
#1

[eluser]smithdp1[/eluser]
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:
Code:
<html>
<head>
<title>Client View</title>
<style type="text/css">

label{
display: block;
}
</style>
</head>
<body>
<h2>Insert</h2>
&lt;?php echo form_open('site/create_client'); ?&gt;
<p>
&lt;input type="checkbox" name="clientid[]" value="10"&gt; - Fred
&lt;input type="checkbox" name="clientid[]" value="20"&gt; - Bob
&lt;input type="checkbox" name="clientid[]" value="30"&gt; - Mary
&lt;input type="hidden" name="campaignid" value="2"&gt;
</p>
<p>
&lt;input type="submit" value="Submit"/&gt;
</p>
&lt;?php echo form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;
Here is the code for my controller:
Code:
&lt;?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:
Code:
&lt;?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!
#2

[eluser]pickupman[/eluser]
When you submit a form with check boxes in the way you have them sent, they are submitted as an array. Only the boxes that are being checked are sent to the server. You should loop through:
Code:
foreach ($this->input->post('clientid[]') as $clientid)
{
    $data = array('clientid' => $clientid,
                            'campaignid' => $this->input->post('campaignid'),
                            'creationdate' => $datetime
                  );
    $this->client->add($data);
}
#3

[eluser]smithdp1[/eluser]
Now i am finally seeing it...I have been trying to get this for 2 days. Here is what I have but I still get 2 erros:

Code:
function create(){
foreach ($this->input->post('clientid[]') as $clientid)
    {
    $data = array(
'clientid' => $clientid,
    'campaignid' => $this->input->post('campaignid'),
    'creationdate' => $datetime
    );
        $this->client->add_record($data);
        $this->index();
}


A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 552


A Database Error Occurred


Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `campaign_to_client` (`clientid`, `campaignid`, `creationdate`) VALUES (Array, '2', '2012-12-20 05:04:23')

Filename: C:\wamp\www\crud\system\database\DB_driver.php

Line Number: 330


Thanks for your help...I was ready to give up...

#4

[eluser]pickupman[/eluser]
Oops minor typo:
Code:
function create(){
foreach ($this->input->post('clientid') as $clientid)
    {
    $data = array(
        'clientid' => $clientid,
        'campaignid' => $this->input->post('campaignid'),
        'creationdate' => $datetime
    );
        $this->client->add_record($data);
        $this->index();
}
#5

[eluser]smithdp1[/eluser]
Worked!!!! Very nice Christmas present for me man. Thank you sooooo much!
#6

[eluser]smithdp1[/eluser]
Ok I knew I was not going to get off that easy. I tried to put this example in my main app and when I submit I get a page not found error. I know all of the code is correct for my app....it looks like in all other areas they are using javascript for submit....so how would I submit this to the controller with javascript?
#7

[eluser]pickupman[/eluser]
You first need to make sure the page will work without JavaScript first. Likely you have the wrong URL in your form tag. You can use the form helper and form_open function for creating the form tag.

Once you get that part working I would suggest using the jquery library. This will allow you to use the [URL="http://docs.jquery.com/Post"]$.post()[/url] method to submit via Ajax.
#8

[eluser]smithdp1[/eluser]
Ok I finally got it going. I just had to make sure my path was correct.

Thanks so much for all your help man a truly appreciate it. Let me know if there is a good tutorial any where and I will check out the javascript/ajax.

Merry Christmas!




Theme © iAndrew 2016 - Forum software by © MyBB