Hello -

I'm new to inserting checkbox arrays into mysql tables. After scouring the web for two days I've found a solution using 'foreach', and it is inserting the data into the table correctly however I'm getting the following Error Notice.

"Notice: Array to String conversion on page blah.php on line 38"
Line 38 of blah.php is referencing a very important function that is the backbone to my form processing.

Here is my checkbox form:

<?php
include 'common/blah.php'; 
if(!db_connect())
die();

//define variables
$pettypes = getvar("pettypes");

//define array
foreach($_POST['pettypes'] as $value){$pets.="$value,";}

//insert query
mysql_query("INSERT INTO pets (housepets) value ('{$pets}')");

?>
<html>
<body>
echo"
<form action ='{$_SERVER['PHP_SELF']}'>
<fieldset><legend>Which Pets do you have in your home</legend>

<input type ='checkbox' name = 'pettypes[]' id='dog' value='dog'><label for='dog'>Dog</label>

<input type ='checkbox' name = 'pettypes[]' id='cat' value='cat'><label for='cat'>Cat</label>

<input type ='checkbox' name = 'pettypes[]' id='hamster' value='hamster'><label for='hamster'>Hamster</label>

<input type ='checkbox' name = 'pettypes[]' id='fish' value='fish'><label for='fish'>Fish</label>

</fieldset>

<p><input type ='submit' name ='submit' value='submit'>

</form>";
</body>
</html>

Here is the function code

function getvar($varname)
{
if(isset(get_magic_quotes_gpc()){
 return trim($_REQUEST[$varname])'
}
 else {
      return trim(addslashes($_REQUEST[$varname]));
   }
}
//variable didn't exsist
return"";
}

Can anybody tell me how to avoid getting the Array to String error? Is there a better way to process my checkbox arrays?

Any help or suggestions would be greatly appreciated.

The function getvar() has the function trim(). trim() operates on a string, and thus will try to convert any parameter it receives to a string. If you therefore pass it an array, it will give you the array to string conversion error.

PHP will cast types depending on the context. If the context requires a string, then it casts the variable to a string. An example of this when you put together strings.

eg:

$age = 100; // int

// $age is cast to a string
$string = 'Jack is '.$age.' years old';

$age = '100'; // string

// age will be cast to an int
$string = 'Joe is '.($age - 1).' years old';

Even though 100 is an integer. It will be cast to a string due to the context it is used in (string concatenation). In the second example, $age will be cast to an int.

The solution to the problem is to make sure you don't pass anything but a string to getvar() or change getvar() to cater for arrays...

Comments
Thanks

Thank you very much.

The function getvar() has the function trim(). trim() operates on a string, and thus will try to convert any parameter it receives to a string. If you therefore pass it an array, it will give you the array to string conversion error.

PHP will cast types depending on the context. If the context requires a string, then it casts the variable to a string. An example of this when you put together strings.

eg:

$age = 100; // int

// $age is cast to a string
$string = 'Jack is '.$age.' years old';

$age = '100'; // string

// age will be cast to an int
$string = 'Joe is '.($age - 1).' years old';

Even though 100 is an integer. It will be cast to a string due to the context it is used in (string concatenation). In the second example, $age will be cast to an int.

The solution to the problem is to make sure you don't pass anything but a string to getvar() or change getvar() to cater for arrays...

This article has been dead for over six months. Start a new discussion instead.