Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FOREACH and INSERT Help

    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:
    Code:
    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.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's no surprise the value being inserted into the "behavior" column isn't changing-- you are using the variable $bid for every insert and it does not change in the foreach loops. How are you able to tell which behavior should be used for each insert?

    Also-- a syntax issue-- you need to change this: $query = "INSERT INTO prior(client_id, behavior, prior) VALUES ('$_POST[client_id]', $bid, $pvalue)"; to this: $query = "INSERT INTO prior(client_id, behavior, prior) VALUES ('{$_POST['client_id']}', $bid, $pvalue)"; .

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    My problem is definately creating the form to define the relationship between "behavior and current" and "behavior and prior".

    Here is some more information of what is to be achieved and what I've done thus far.

    This is an update form. The data gets stored by a form which is previously submitted. The data when stored for behavior, current, and prior used the implode function so it is stored as a single string.

    The update form, which some of the form code will appear below, pre-populates the checkboxes from the table using the explode function. If changes need to be made to the selections, the person can uncheck/check whatever needs to be.

    The trick is that the admin person wants to be able have some reporting capabilities. For example, count the number of clients by behavior. Since the original data is stored as a single string it would be a nightmare from which to report.

    So, I created three tables, behavior, current, and prior to store the data for reporting. Each table will have multiple records for each client since a client will or can have multiple behaviors.

    The form code which allows the admin person to check off the behavior and the current/prior values for each behavior looks like this:
    Code:
    <table class="apptable">
    <tr><th scope="col">Behavior</th><th scope="col">Current</th><th scope="col">Past</th></tr>
    <tr><td class="i">
    <input type="checkbox" name="behavior1[]" value="Abuse"<?php echo ( isset ( $_SESSION['behavior1'] ) && in_array('Abuse', $_SESSION['behavior1']) ? ' checked="checked"' : '' ); ?> />Abuse&nbsp;&nbsp;</td>
    <td class="h">
            <input type=checkbox name=current1[] value="Physical"<?php echo ( isset ( $_SESSION['current1'] ) && in_array('Physical', $_SESSION['current1']) ? ' checked="checked"' : '' ); ?> />Physical&nbsp;&nbsp;
            <input type=checkbox name=current1[] value="Emotional"<?php echo ( isset ( $_SESSION['current1'] ) && in_array('Emotional', $_SESSION['current1']) ? ' checked="checked"' : '' ); ?> />Emotional<br>
            <input type=checkbox name=current1[] value="Sexual"<?php echo ( isset ( $_SESSION['current'] ) && in_array('Sexual', $_SESSION['current1']) ? ' checked="checked"' : '' ); ?> />Sexual&nbsp;&nbsp;&nbsp;&nbsp;
            <input type=checkbox name=current1[] value="Recent"<?php echo ( isset ( $_SESSION['current1'] ) && in_array('Recent', $_SESSION['current1']) ? ' checked="checked"' : '' ); ?> />Recent<br></td>
    <td class="h">
            <input type=checkbox name=prior1[] value="Physical"<?php echo ( isset ( $_SESSION['prior1'] ) && in_array('Physical', $_SESSION['prior1']) ? ' checked="checked"' : '' ); ?> />Physical&nbsp;&nbsp;
            <input type=checkbox name=prior1[] value="Emotional"<?php echo ( isset ( $_SESSION['prior1'] ) && in_array('Emotional', $_SESSION['prior1']) ? ' checked="checked"' : '' ); ?> />Emotional<br>
            <input type=checkbox name=prior1[] value="Sexual"<?php echo ( isset ( $_SESSION['prior1'] ) && in_array('Sexual', $_SESSION['prior1']) ? ' checked="checked"' : '' ); ?> />Sexual&nbsp;&nbsp;&nbsp;&nbsp;
            <input type=checkbox name=prior1[] value="Recent"<?php echo ( isset ( $_SESSION['prior1'] ) && in_array('Recent', $_SESSION['prior1']) ? ' checked="checked"' : '' ); ?> />Recent<br></td>
    </tr> </table>
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Update Record" class="btn" >
    <input type="hidden" name="submitted" value="TRUE" />
    
    </form>
    In the end, after the form is submitted, the resulting tables should appear like this:
    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 | prio
    1 | 4 | Eating Disorder | Emotional
    2 | 4 | Eating Disorder | Verbal
    3 | 4 | Eating Disorder | Anorexia

    My problem in coding the form is defining the relationship between "behavior and current" and "behavior and prior". I can't seem to get the arrays created to give me the correct response.

    Any ideas?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I'm still missing a piece of the puzzle because I only see one possible checkbox option for behavior and that is "Abuse", and there are no separate checkboxes for behavior for current and prior (so checking "abuse" applies to both current and prior).

    Perhaps you should have just one table to store both "current" and "prior" behaviors. Add a column called "time_period" or whatever, and each row would store either "current" or "prior" in that column.

    It also occurs to me that the "abuse" and "eating disorder" value are mutually exclusive for the most part-- the current value "verbal" always falls under the "abuse" behavior, never the "eating disorder" behavior. Or am I wrong about that? If that is the case, then you don't even need to store "abuse" or "eating disorder", because that value will be implied based on the other value (verbal/physical/sexual is always an abuse, while anorexia is always an eating disorder).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •