CodeIgniter Forums
Array to string conversion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Array to string conversion (/showthread.php?tid=38816)



Array to string conversion - El Forum - 02-19-2011

[eluser]gigas10[/eluser]
Hey everyone, I'm confused on where to post now, so hopefully I can post this here, I'm also using 1.7.2 still.

Anyway, I'm trying to loop through a post array and input the data into a table, simple enough.

However, I'm getting an Array to string conversion error even though the data submits into the table as it should. I am confused.

Code for the form
Code:
<strong>Work Order Number(s): </strong>
<br />
&lt;?=form_hidden('id', '1');?&gt;
&lt;?=form_input("work_order[]");?&gt;
<br /><br />
<div id="here"></div>
<br />
<a href="#">(+add another work order number)</a>

Here is the code to loop through the input array and submit the data to the table.
Code:
foreach($this->input->post('work_order') AS $value){
    $w_input = array();
    $w_input['record_id'] = $id;
    $w_input['work_order'] = $value;
    $this->db->insert('smf_work_order_numbers', $w_input);
}

And here is the exact error.
Code:
A PHP Error was encountered
Severity: Notice

Message: Array to string conversion

Filename: mssql/mssql_driver.php

Line Number: 496

A Database Error Occurred
Error Number:


INSERT INTO smf_work_order_numbers (record_id, work_order) VALUES ('32', Array)

Keep in mind, the code works and the data is submitted to the table successfully. I don't understand why I get this error, because the $value is not an array.


Array to string conversion - El Forum - 02-19-2011

[eluser]InsiteFX[/eluser]
Try this:
Code:
$w_input = array();

foreach($this->input->post('work_order') AS $key => $value)
{
    $w_input[$key] = $value;
}

$this->db->insert('smf_work_order_numbers', $w_input);

InsiteFX


Array to string conversion - El Forum - 02-22-2011

[eluser]gigas10[/eluser]
A Database Error Occurred
Error Number:


INSERT INTO smf_work_order_numbers (record_id, 2) VALUES ('32', 'test4')


it's swapping the key with the index of the array, so naturally instead of $w_input[$key], $w_input['work_order'] should work, but then I just get the array error instead... i don't understand why it says this even though it submits to the database just fine....


Array to string conversion - El Forum - 02-22-2011

[eluser]gigas10[/eluser]
I gave up and ended up just setting the db debug variable in the database class to false.