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.