Hello:
I was wondering if someone can help me out. I have a form which creates three arrays: behavior[], current[], and prior[].
I need to store the data into two tables: current and prior. I have a third table called behavior which stores the behavior and client_id. All three of these tables will have multiple records per client_id.
The variable behavior is the top level or root array. This variable will store info as Abuse, Fire Setting, Eating Disorder, etc.
The current and prior variables are the child variables.
The current table will have the fields: id, client_id, behavior, current
The prior table will have the fields: id, client_id, behavior, prior
I have three foreach statements, one for each array, which inserts the data into the tables. The foreach statement for behavior works perfectly. The foreach statements for current and prior are not working. The "behavior" field is causing the problem. When I look at my tables, the data for current and prior are correct but the field for behavior shows the last entry from the table for each record.
Here is an example of how the data looks in the tables:
behavior table:
id | client_id | behavior
1 | 4 | Abuse
2 | 4 | Eating Disorder
current table:
id | client_id | behavior | current
1 | 4 | Eating Disorder | Physical
2 | 4 | Eating Disorder | Verbal
3 | 4 | Eating Disorder | Anorexia
prior table:
id | client_id | behavior | current
1 | 4 | Eating Disorder | Emotional
2 | 4 | Eating Disorder | Verbal
3 | 4 | Eating Disorder | Anorexia
The current and prior tables are incorrect. They should show:
current table:
id | client_id | behavior | current
1 | 4 | Abuse | Physical
2 | 4 | Abuse | Verbal
3 | 4 | Eating Disorder | Anorexia
prior table:
id | client_id | behavior | current
1 | 4 | Abuse | Emotional
2 | 4 | Abuse | Verbal
3 | 4 | Eating Disorder | Anorexia
These are my statements:
foreach($_POST['behavior'] as $bvalue){
$query = "INSERT INTO behavior(client_id, behavior) VALUES ('$_POST[client_id]', $bvalue)";
$result = mysql_query($query) or die(mysql_error());
$bid = mysql_insert_id();
foreach($_POST['current'] as $cvalue){
$query = "INSERT INTO current(client_id, behavior, current) VALUES ('$_POST[client_id]', $bid, $cvalue)";
$result = mysql_query($query) or die(mysql_error());
foreach($_POST['prior'] as $pvalue){
$query = "INSERT INTO prior(client_id, behavior, prior) VALUES ('$_POST[client_id]', $bid, $pvalue)";
$result = mysql_query($query) or die(mysql_error());
Can someone help me with this problem? When I print the individual arrays, the data in the arrays are correct. The insert isn't working properly.
Thank you for the help.