What is causing this error?
I have a form for users that queries the database and retrieves values, if they've already been set. Upon resubmission, the database is updated with the new information. I have a number of arrays, but I keep getting this error message. Everything looks fine when I print_r ($_POST), so, I'm not sure what the problem is. Here are some snippets of the code:
/************************
** Here is one array that is
** built based on values set
** in the database
************************/
/* **** Color radio buttons, pre-populated if necessary**** */
$result = mysql_query('SELECT * FROM thecolortable WHERE ID = ' . $_GET['colorid'])
or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
if ($row['Color_Name'] == '2') {
echo '<input type="radio" name="color[]" value="2" CHECKED>2';
echo '<input type="radio" name="color[]" value="1">1';
echo '<input type="radio" name="color[]" value="0">0';
} elseif ($row['Color_Name'] == '1') {
echo '<input type="radio" name="color[]" value="2">2';
echo '<input type="radio" name="color[]" value="1" CHECKED>1';
echo '<input type="radio" name="color[]" value="0">0';
} elseif ($row['Color_Name'] == '0') {
echo '<input type="radio" name="color[]" value="2">2';
echo '<input type="radio" name="color[]" value="1">1';
echo '<input type="radio" name="color[]" value="0" CHECKED>0';
} else {
echo '<input type="radio" name="color[]" value="2">2';
echo '<input type="radio" name="color[]" value="1">1';
echo '<input type="radio" name="color[]" value="0">0';
}
} /* **** End of Color While Statement **** */
?>
<?
/*********************
** Here's another one that
** includes a text box as part
** of it, in the event a user
** wants to add a value
*********************/
/* **** Groceries checkboxes, pre-populated if necessary**** */
$result = mysql_query('SELECT Category FROM thecolortable WHERE ID = ' . $_GET['color'])
or exit(mysql_error());
$cat_exp = explode(',', mysql_result($result, 0));
foreach ($groceries as $key => $value1)
{
echo '<input type="checkbox" name="groceries[]" value="' . $value1 . '"';
if (in_array($value1, $cat_exp)) {echo ' checked';}
echo '>' . $value1 . '<br>';
}
?>
<br />Add a grocery:<br /> <input type="text" name="addgrocery" size="15"
value="<?php if (isset($_POST['addgrocery'])) echo $_POST['addgrocery']; ?>">
<br /><font color="#666666">To add a new grocery, just type it in the box</font>
?>
And here's the portion of the script that actually validates / deals with these arrays:
/***********************
** For the first one
***********************/
if (isset($_POST['color'])) {
$color = escape_data($_POST['color']);
} else {
$color = FALSE;
echo '<font color="red">Please set the color name</font>';
}
/***********************
** For the second one
***********************/
if (!empty($_POST['addgrocery'])) {
$addgrocery = trim($_POST['addgrocery']);
$query = "INSERT INTO Grocery_Menu (Grocery_Name) VALUES ('$addgrocery')";
$result = @mysql_query($query);
} else {
$addgrocery = '';
}
if (!isset($_POST['groceries']) && (empty($_POST['addgrocery']))) {
$groceries=FALSE;
echo '<font color="red">Please select at least one grocery</font>';
} elseif (!isset($_POST['groceries']) && (!empty($_POST['addgrocery']))) {
$groceries=$_POST['addgrocery'];
} else {
$groceries=implode(',',$_POST['groceries']).','.$addgrocery;
}
And if I print_r ($_POST), I get:
Array ( [color] => Array ( [0] => 0 ) [groceries] => Array ( [0] => milk ) [submit] => Save )
So, why the array to string conversion error?