Hello:
I have a form which consists of two array variables (current and prior). I need to insert the results into a table. When I run my script, the table is empty.
I did some testing/troubleshooting to see if my values are coming through and they are. I echoed all my form variables and the results were correct. I am having difficulty inserting the results into a table. I need to insert both array variables (current and prior). I first attempted to try current and my table was empty. I don't know how to even begin to include the second array in my query for the insert.
Can someone help me out? I commented by query statements because I was testing my variables.
My table has four columns: id, behavior_name, current, prior. Here is my code.
Thank you for the help.
<?php
require_once ('mysql_connect.php');
if (isset($_POST['submitted'])) {
//validate form data
if ( !empty ( $_POST['behavior']) ) {
$behavior = escape_data($_POST['behavior']);
} else {
$behavior = FALSE;
}
if ( isset ( $_POST['current']) && (is_array($_POST['current']))) {
$current = TRUE;
} else {
$current = NULL;
}
if ( isset ( $_POST['prior']) && (is_array($_POST['prior']))) {
$prior = TRUE;
} else {
$prior = NULL;
}
if ($behavior && $current && $prior) {
echo $behavior;
foreach ($_POST['current'] as $value)
{
echo "<li>$value</li>\n";
}
echo'</ul>';
foreach ($_POST['prior'] as $vprior)
{
echo "<li>$vprior</li>\n";
}
echo'</ul>';
//$query='INSERT INTO behavior(behavior_name, current, prior)
// VALUES ';
// foreach($_POST['current'] as $cvalue) {
// $query .="($behavior, $cvalue), ";
// }
// $query = substr($query,0,-2);
// $result=@mysql_query($query);
}
}
else {
echo 'No good.';
}
?>
<form action="behavior_form_test.php" method="post">
<table>
<tr><td valign="top">
<input type="checkbox" name="behavior" id="Abuse" value="Abuse">Abuse</td>
<td align="top"><input type="checkbox" name="current[]" id="Physical" value="Physical">Physical<br>
<input type="checkbox" name="current[]" id="Emotional" value="Emotional">Emotional<br>
<input type="checkbox" name="current[]" id="Other" value="Other">Other<br><br></td>
<td align="top"><input type="checkbox" name="prior[]" id="Physical" value="Physical">Physical<br>
<input type="checkbox" name="prior[]" id="Emotional" value="Emotional">Emotional<br>
<input type="checkbox" name="prior[]" id="Other" value="Other">Other<br><br></td>
</tr>
<tr><td><input type="checkbox" name="behavior" id="Eating Disorder" value="Eating Disorder">Eating Disorder</td></tr>
<tr><td><input type="checkbox" name="behavior" id="Aggressive" value="Aggressive">Aggressive</td></tr>
</table>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn" >
<input type="hidden" name="submitted" value="TRUE" />