Forum Moderators: coopster

Message Too Old, No Replies

Array to string conversion Notice

         

dbarasuk

3:17 pm on Apr 5, 2008 (gmt 0)

10+ Year Member



I have in the middle of my script an array variable designed to hold values coming from checkboxes (which can be more than one value) like this:

$payment_means=array();// array initialization.
$payment_means=mysqli_real_escape_string($connection, strip_tags(trim($_POST['payment_means'])));// of course in the HTML, all checkboxes have same name "payment_means[]"
$payment_means=implode(',', $payment_means);
// check if payment means is array
if(!is_array($payment_means)) $payment_means=explode(',', $payment_means); // this is in case for example the user clicks only one checkbox. The second array value would be an empty string, but empty string is allowed in database, so no problem at that level.
// check values to be sent to database.
if(!isset($date) ¦¦ !isset($expense_amount) ¦¦ !isset($expense_reason) ¦¦ !isset($location) ¦¦ !isset($payment_means))
{
echo "<pre>You didn't fill in all the fields</pre>";
exit();
}

When I run the whole script I get an "Array to string conversion" notice just from the second line of the script above like this:

"Notice: Array to string conversion in C:\htdocs\MySQL_queries\expense_checkbox.php on line 17

Warning: implode() [function.implode]: Bad arguments. in C:\htdocs\MySQL_queries\expense_checkbox.php on line 18
Query failed!"

Any idea of how to solve this? I am thinking of a way of assigning the values into the array so that no conversion is needed just in the way $payment_means would be = array("value1", "value2",..., "value N"); I think this way would give no warning for the implode() function as shown above.

Please take one minute to let me know what I should do.

Regards,

coopster

7:52 pm on Apr 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mysql_real_escape_string [php.net] expects two parameters. The first is a string and the second is the connection resource. You have them backwards. Also, you are passing it an array, not a string. You need to loop through the posted array values and escape them.

dbarasuk

1:10 am on Apr 6, 2008 (gmt 0)

10+ Year Member



imploding $payment_means into a new variable like this: $means=implode(',',$payment_means) instead of $payment_means=implode($payment_means))eliminated the notice that was causing the trouble.

Thanks for your help.