...

View Full Version : Form Design to Table Insert Help



focus310
05-30-2007, 05:34 PM
Hello:

I have a form which appears as the image that is attached. I need to get the results of the form into a table. I am having great difficulty.

I have two tables.
Current
current_id
client_id
name
current

Prior
prior_id
client_id
name
prior

I attempted the following. I created the checkboxes as arrays, behavior[], current[], and prior[]. The insert didn't work correctly. I can't even begin to describe what the result of the insert query was.

Does anyone have any ideas on how I can proceed to capture the data from this form?

Thanks for the help.

mlseim
05-30-2007, 05:53 PM
I assume you're using PHP, since this is the PHP forum.
Do you have some PHP code to show us? Your form "arrays"
are sending the variables to the PHP script ... we need to see
your HTML for your form and your PHP script (or at least the
first part where you read your variables).

focus310
05-30-2007, 06:57 PM
Hi,

Here is a snippet of the code. The form is very long. I only included the top two behaviors as you see on the image. All others follow the same syntax.



<?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 ($behavior && $current) {

$query='INSERT INTO behavior(behavior_name, current)
VALUES ';
foreach($_POST['current'] as $cvalue) {
$cvalue = escape_data($cvalue);
$query .="('$behavior', '$cvalue'), ";
}
$query = substr($query,0,-2);
$result=@mysql_query($query) or die(mysql_error());

}
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="Sexual" value="Sexual">Sexual<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="Sexual" value="Sexual">Sexual<br>
<input type="checkbox" name="prior[]" id="Other" value="Other">Other<br><br></td>
</tr>

<tr><td><input type="checkbox" name="behavior" id="Aggression" value="Agression">Aggression</td>
<td align="top"><input type="checkbox" name="current[]" id="Physical" value="Physical">Physical<br>
<input type="checkbox" name="current[]" id="Verbal" value="Verbal">Verbal<br>
<input type="checkbox" name="current[]" id="Object" value="Object">Object<br>
<input type="checkbox" name="current[]" id="Passive" value="Passive">Passive<br><br></td>

<td align="top"><input type="checkbox" name="prior[]" id="Physical" value="Physical">Physical<br>
<input type="checkbox" name="prior[]" id="Verbal" value="Verbal">Verbal<br>
<input type="checkbox" name="prior[]" id="Object" value="Object">Object<br>
<input type="checkbox" name="prior[]" id="Passive" value="Passive">Passive<br><br></td>

</tr>

</table>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn" >
<input type="hidden" name="submitted" value="TRUE" />


My insert does the following. The first time the script is run and if you select:
Abuse->Physical & Emotional; the insert into the table is correct.

Now, if you were to do the following:
Abuse->Sexual
Aggression->Verbal and Physical; the insert shows the following result:

Aggression - Sexual
Aggression - Verbal
Aggression - Physical

The table should show:
Abuse - Sexual
Agression - Verbal
Agression - Physical

The insert seems to be inserting the behavior name of the last chosen one. In this case, aggression is the second behavior and aggression is being applied to all the choices above itself.

Now, I'm only testing right now for current. I need to have the same functionality for prior.

Can you help me out?

mlseim
05-30-2007, 08:04 PM
Not actually testing anything myself, I'm questioning why you have
checkbox types with the same names?

example,

<input type="checkbox" name="behavior" id="Abuse" value="Abuse">

and

<input type="checkbox" name="behavior" id="Aggression" value="Agression">

If you're checking for $_POST['behavior']
how do you know which one of the two will be true?
... if I check both of the two boxes, won't it always be "Agression" (spelling),
which is the 2nd checkbox of the same name?

I think you need unique names.

Maybe someone else might see something I'm missing.

focus310
05-30-2007, 08:38 PM
Hi,

When I first designed the form, the code looked as such:



<input type="checkbox" name="abuse" id="Abuse" value="Abuse">Abuse

<input type=checkbox name=abuse_curr[] value="Physical">Physical
<input type=checkbox name=abuse_curr[] value="Emotional">Emotional
<input type=checkbox name=abuse_curr[] value="Sexual">Sexual
<input type=checkbox name=abuse_curr[] value="Recent">Recent

<input type=checkbox name=abuse_pr[] value="Physical">Physical
<input type=checkbox name=abuse_pr[] value="Emotional">Emotional
<input type=checkbox name=abuse_pr[] value="Sexual">Sexual
<input type=checkbox name=abuse_pr[] value="Recent">Recent

<input type="checkbox" name="aggression" id="Aggression" value="Aggression">Agression
<input type=checkbox name=agg_curr[] value="Physical">
<input type=checkbox name=agg_curr[] value="Verbal">Verbal
<input type=checkbox name=agg_curr[] value="Object">Object
<input type=checkbox name=agg_curr[] value="Passive">Passive
<input type=checkbox name=agg_curr[] value="Peer">Peer
<input type=checkbox name=agg_curr[] value="Adult">Adult
<input type=checkbox name=agg_curr[] value="Younger Child">Younger Child

<input type=checkbox name=agg_pr[] value="Physical">Physical
<input type=checkbox name=agg_pr[] value="Verbal">Verbal
<input type=checkbox name=agg_pr[] value="Object">Object
<input type=checkbox name=agg_pr[] value="Passive">Passive
<input type=checkbox name=agg_pr[] value="Peer">Peer
<input type=checkbox name=agg_pr[] value="Adult">Adult
<input type=checkbox name=agg_pr[] value="Younger Child">Younger Child


I highlighted in red the unique names for behavior. Then, each behavior had its own name for current and prior.

My problem was inserting the data into a table so it would appear like so (provided these were the selections made from the form):

I determined that I needed two tables: one for current and one for prior because there can be a mis-match between the two. For example, in prior the person could have been both Emotionally and Physically abused. In the current group, the person is probably only Emotionally abused. You see this is a mis-match for the same behavior so I thought putting current and prior seperately was the right thing to do.

My table structure for current looks like so:
current_id
user_id
behavior_name
current

The results should appear like so in the table after submission of the form:
1,1,Abuse,Physical
2,1,Abuse,Emotional
3,1,Aggression,Peer
4,1,Aggression,Adult

I was not able to get my query to insert that data to appear as above using the coding scheme I used for my form which is above.

I am open to any ideas and suggestions on how I can improve the form coding or anything else.

Thanks for the help.

Fumigator
05-30-2007, 09:26 PM
I believe if you were to display the $_POST array after the form is submitted using print_r you will be able to figure out how to convert the form data over to the database.



print "<pre>";
print_r($_POST);
print "</pre>";
die();


Do that, and you'll see how those checkbox values come out in the wash.

(It will also help me if you post the results here)

mlseim
05-30-2007, 09:26 PM
Here's the way I am seeing it ...

current/prior , behavior , detail
=================================
current,abuse,physical
current,abuse,emotional
current,abuse,sexual
current,abuse,recent
current,aggression,physical
current,aggression,verbal
current,aggression,object
current,aggression,passive
current,aggression,peer
current,aggression,adult
current,aggression,younger
current,aggression,child
current,allegations,peers
current,allegations,adults
current,allegations,males
current,allegations,females
current,allegations,recent
current,allegations,history
current,anxiety,mild
current,anxiety,moderate
current,anxiety,severe
current,assaultive,peer
current,assaultive,adult
current,assaultive,weapons
current,assaultive,younger
current,attention,hyperactivity
current,cruelty,yes
current,cruelty,no
prior,abuse,physical
prior,abuse,emotional
prior,abuse,sexual
prior,abuse,recent
prior,aggression,physical
prior,aggression,verbal
prior,aggression,object
prior,aggression,passive
prior,aggression,peer
prior,aggression,adult
prior,aggression,younger
prior,aggression,child
prior,allegations,peers
prior,allegations,adults
prior,allegations,males
prior,allegations,females
prior,allegations,recent
prior,allegations,history
prior,anxiety,mild
prior,anxiety,moderate
prior,anxiety,severe
prior,assaultive,peer
prior,assaultive,adult
prior,assaultive,weapons
prior,assaultive,younger
prior,attention,hyperactivity
prior,cruelty,yes
prior,cruelty,no

=======================================

You stated you have two tables:



I have two tables.
Current
current_id
client_id
name
current

Prior
prior_id
client_id
name
prior


So, with your form, you need to first determine
if it's prior or current, then what behavior, then what detail

I'm now thinking more about what the form might look like ...

In fact, if you created a "pull-down" option list <select> instead
of the checkboxes, you could create an actual variable just like the
list I've made above.

Perhaps these ideas might trigger something new in your mind.

I'm thinking more of <select> <option> ... instead of checkboxes
as a better way to collect your data.

focus310
05-30-2007, 10:13 PM
Hi,

I printed the results of the array and I got the following:
Array
(
[behavior] => Abuse
[abuse_curr] => Array
(
[0] => Physical
[1] => Sexual
)

[abuse_pr] => Array
(
[0] => Physical
[1] => Sexual
)

I'm not quite following why the word "Array" appears after [abuse_curr] and [abuse_pr]. Then, it shows the values.

aedrin
05-30-2007, 11:18 PM
[abuse_curr] => Array
(
[0] => Physical
[1] => Sexual
)

The value of the 'abuse_curr' index is an array with the values Physical and Sexual.

PHP just inserts a new line there for clarity.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum