Hello all,

I am needing some guidance on how to build an array from POST Variables an then insert the data into mysql. Here is what I have

M Variable from submitted form

$parentid ="7638sds";

$child3_1_name =$_POST['chld3_1_name'];
$child3_1_age =$_POST['child3_1_age'];

$child3_2_name =$_POST['chld3_2_name'];
$child3_2_age =$_POST['child3_2_age'];

$child3_3_name =$_POST['chld3_3_name'];
$child3_3_age =$_POST['child3_3_age'];

 $cnt="3";

$childrenContainer ="
(1) Child Name : $child3_1_name -- Age: $child3_1_age \n
(2) Child Name : $child3_2_name -- Age: $child3_2_age \n
(3) Child Name : $child3_3_name -- Age: $child3_3_age \n
";

My mysql insert would insert each child info (consisting of name and age) and linked by the parent id
the statement is constructed as:

        $insertChildCount ="INSERT childrenCount SET parentid='$parentid', childrenCtn='1', childName='$childName',  childAge='$childOnly_age', childrenInfo='$childrenContainer'"; 

So essentially in the provided POST Variables, the sql would insert three children into the DB, all with the same parentid identifier.

Any thoughts on this is greatly appreciated!

Mossa

when naming your form fields add brackets to the name to indicate an array
<input name="child[1]['name']"/>

Jstfsklh211,

thanks for the reply!

my form data is gather in the following way:

<ol id="thirdChild" class="formset" >
<div id=oneChild style="position:relative; border-left:1px solid grey; left:-50px; top:0px; width:100%; background:<?php echo $appColor_lightblue;?>; color:white">Enrolling Three Children! </div>

                            <div align=center width=100% >
                    <br />

                            <label for='child3_1_age'>1st Child:</label>
                            <select name="child3_1_age"  class='textbox_sel' style="height:40px">
                            <option value="0">--Select Child's Age:--</option>
                            <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
                            <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
                            <option value="18-24 MTHS">18 MTHS - 24 MTHS</option>
                            <option value="2 YRS">2 YRS</option>
                            <option value="3 YRS">3 YRS</option>
                            <option value="4 YRS">4 YRS</option>
                            <option value="5-12 YRS">5-12 YRS</option>

                            </select>                        
                            <input type="input" name="chld3_1_name" value="" placeholder="1st Child's Name" class="textbox_child" style="padding-left:30px">

                            <br />
                            <table width=50%>
<tr>
    <td style='position:relative; top: 0px;  border-bottom:2px dotted grey; padding:5px'></td>
</tr>
</table>
                            <label for='child3_2_age'>2nd Child:</label>
                            <select name="child3_2_age"  class='textbox_sel' style="height:40px">
                            <option value="0">--Select Child's Age:--</option>
                            <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
                            <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
                            <option value="18-24 MTHS">18 MTHS - 24 MTHS</option>
                            <option value="2 YRS">2 YRS</option>
                            <option value="3 YRS">3 YRS</option>
                            <option value="4 YRS">4 YRS</option>
                            <option value="5-12 YRS">5-12 YRS</option>

                            </select>                        
                            <input type="input" name="chld3_2_name" value="" placeholder="2nd Child's Name" class="textbox_child" style="padding-left:30px">
                            <br />
                        <table width=50%>
<tr>
    <td style='position:relative; top: 0px;  border-bottom:2px dotted grey; padding:5px'></td>
</tr>
</table> 
                            <label for='child3_3_age'>3nd Child:</label>
                            <select name="child3_3_age"  class='textbox_sel' style="height:40px">
                            <option value="0">--Select Child's Age:--</option>
                            <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
                            <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
                            <option value="18-24 MTHS">18 MTHS - 24 MTHS</option>
                            <option value="2 YRS">2 YRS</option>
                            <option value="3 YRS">3 YRS</option>
                            <option value="4 YRS">4 YRS</option>
                            <option value="5-12 YRS">5-12 YRS</option>

                            </select>                        
                            <input type="input" name="chld3_3_name" placeholder="3rd Child's Name" class="textbox_child" style="padding-left:30px">
                    </div>


</ol>

---

Does your suggestion still apply?

my freind read the introduction part than apply the solution please.

as i see your code i think what you want to do, is this

that parent id will be the same, only the child age, and child name will change. means your table should look like this in the localhost

parent id==parent7638sds child name=name in the first input box, child age=age in the first select box,

parent_id==same again parent7638sds, name=name in the second input box, child age=age in the second select box.

if this how you want carry on.

i make some changes to your code as well in order to keep my reply short, instead of using 8 select option from the selection box i will only use two, and you have repeated your code three time i will only create two child boxes, because even you use more selection option code will work so does not matter how many option i used for solution. and i also remove your most of the css.

hope you understand what i am trying to say as english is not my first language.

your code should look like this in the form section

<form method="post" action="test.php">

    <select name="age[]" >
        <option value="0">--Select Child's Age:--</option>
        <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
        <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
        </select>
    <input type="text" name="name[]"/>
    <br><br><br>

    <select name="age[]" >
        <option value="0">--Select Child's Age:--</option>
        <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
        <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
        </select>
    <input type="text" name="name[]"/>
    <br><br><br>

    <select name="age[]" >
        <option value="0">--Select Child's Age:--</option>
        <option value="6 WkS-12 MTHS">6 WKS-12 MTHS</option>
        <option value="12-18 MTHS">12 MTHS - 18 MTHS</option>
        </select>
    <input type="text" name="name[]"/>
    <br><br><br>

    <input type="submit">

</form>

you made some mistake in this section which i think is worth mentioning, first you did not start form and did not give submit button. as you can see in the solution, form method is post and the action="test.php";

and there is no type="input" which you used in the form part

read this line in your code

<input type="input" name="chld3_2_name" value="" placeholder="2nd Child's Name" class="textbox_child" style="padding-left:30px">

and second if you want your value to be store in the form of an array you have to tell like this value="name[]" than you can retreive it from array,
means first you need to store than you can get it.

like you cant do that

<select name="child3_1_age"  class='textbox_sel' style="height:40px">

if you check the solution in the form part, select option for age look like this at every place

<select name="age[]"  class='textbox_sel' style="height:40px">

and for the name look like this

<input type="text" name="name[]" value=""  class="textbox_child" style="padding-left:30px">

because i want to store every child age and every name in the age array and in the name array respectivly.

so now look at the test.php code

<?php

//first connect with the database
//also remember i did not give database name, and password, you can
//simple replace them with your value
$con=  mysqli_connect('localhost', 'root', 'your password', 'your database name');



for($i=0;$i<count($_POST['name']);$i++)

//now we ran the for loop, the resean behind $i<count($_post)['name'],
//is this if in the future you want to add more child to your form you 
//dont have to change the code, it simple gonna check how many child //name you got in the form, 
//and will run the loop accordingly.
//so its good to give dynamic value instead of fix number

//now we can simply set the variable and insert them into ourt database
{
    $name=$_POST['name'][$i];
    $age=$_POST['age'][$i];
    $query="INSERT INTO DAY (parent_id,child_name,child_age) Values
            ('7638sds','".$name."','".$age."')

            ";

    $result=  mysqli_query($con, $query)or die(mysqli_error($con));
}

my freind and if I got your question wrong dont mind i just try to help.

Arif_suhail_123,

I appreciate your time and expertise on this post. I have not had a chance to implement what you have suggested, but I just wanted say thanks for your thoughts on the matter.

I will post back the outcome.

Thanks again,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.