Jump to content

insert array into mysql database


noobdood
 Share

Recommended Posts

hi guys so i have this add contacts page and the form is divided into 3 different froms 1) primary contact 2)spouse 3)child and the child form data is inserted as array into database because in the primary contact part of the form there is a "Children ?" with yes and no radio button and if yes a drop down list is enabled where if user chooses say 2 then there would be 2 child form that appears. and since theres 2 children then in the database a new row and data will be added accordingly.

 

image attached to be clearer.

 

post-168918-0-03446700-1400410339_thumb.jpg


i got it inserted into database but in the specified field it says array:

 

|child_name|dob|house_add1|mobile|office|email|
inserted:
|array|array-array-array|array|array|array|array|

 

query:

"INSERT INTO child VALUES('','".$childsalutations." $childfname $childlname',' ".$cday."-".$cmonth."-$cyear ','$childline1','$childline2','$childm','$childoff','$childemail')"

in a stackoverflow question(not my own question) someone says: information stating arrays need to be split, before inserting into the table. does that mean something like this?:

$cday = ($_POST['cday']);
$cmonth = ($_POST['cmonth']);
$cyear = ($_POST['cyear']);
$childsalutations = ($_POST['child-salutations']);
$childfname = ($_POST['child-fname']);
$childlname = ($_POST['child-lname']);
$childline1 = ($_POST['child-line1']);
$childemail = ($_POST['child-email']);
$childm = ($_POST['child-mobile']);
$childoff = ($_POST['child-office']);

$info = array('c_name' => $childsalutation $childfname $childlname, 'c_dob' => $cday-$cmonth-$cyear, 'c_line1' => $childline1, 'c_mobile' => $childm, 'c_office' => $childoff, 'c_email' => $childemail)

just in case u wanted to c the html child form(warning its abit long,very!):

<table class="prime">
	<tbody>
	<br>
	<tr><td style="font-size:20px;font-weight:bold">Child <span id="number"></span></td></tr>
	<tr>
		<td>Salutation :</td> 
		<td><select name="child-salutations[]" id="child-salutations">
				<option value="" disabled selected>Salutations</option>
				<option value="Datin">Datin</option>
				<option value="Datin Paduka">Datin Paduka</option>
				<option value="Dato Paduka">Dato Paduka</option>
				<option value="Dato'">Dato'</option>
				<option value="Dato' Seri">Dato' Seri</option>
				<option value="Datuk">Datuk</option>
				<option value="Datuk Seri">Datuk Seri</option>
				<option value="Dr.">Dr.</option>
				<option value="Haji">Haji</option>
				<option value="Hajjah">Hajjah</option>
				<option value="HM">HM</option>
				<option value="HRH">HRH</option>
				<option value="Miss">Miss</option>
				<option value="Mrs.">Mrs.</option>
				<option value="Mr.">Mr.</option>
				<option value="Pehin">Pehin</option>
				<option value="Professor">Professor</option>
				<option value="Raja">Raja</option>
				<option value="Tan Sri">Tan Sri</option>
				<option value="Tengku">Tengku</option>
				<option value="Tuanku">Tuanku</option>
				<option value="Tun">Tun</option>
				<option value="Tunku">Tunku</option>
				<option value="Ungku">Ungku</option>
				</select>
		</td>
	</tr>
	<tr><td colspan="2"><label class="label" style="color:Red">*If a person has many salutations, choose the highest form of salutation</label></td></tr>
	<tr><td>First Name :</td><td><input type="text" name="child-fname[]" id="child-fname" class="style" /></td>
			<td>Last Name :</td><td><input type="text" name="child-lname[]" id="child-lname" class="style" /></td></tr>
	<tr>
		<td>Date of Birth : </td>
		<td>
			<select name="cday[]">
			<option value=""selected disabled>Day</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
			<option value="6">6</option>
			<option value="7">7</option>
			<option value="8">8</option>
			<option value="9">9</option>
			<option value="10">10</option>
			<option value="11">11</option>
			<option value="12">12</option>
			<option value="13">13</option>
			<option value="14">14</option>
			<option value="15">15</option>
			<option value="16">16</option>
			<option value="17">17</option>
			<option value="18">18</option>
			<option value="19">19</option>
			<option value="20">20</option>
			<option value="21">21</option>
			<option value="22">22</option>
			<option value="23">23</option>
			<option value="24">24</option>
			<option value="25">25</option>
			<option value="26">26</option>
			<option value="27">27</option>
			<option value="28">28</option>
			<option value="29">29</option>
			<option value="30">30</option>
			<option value="31">31</option>
		</select>
		<select name="cmonth[]">
			<option value="" selected disabled>Month</option>
			<option value="1">January</option>
			<option value="2">February</option>
			<option value="3">March</option>
			<option value="4">April</option>
			<option value="5">May</option>
			<option value="6">June</option>
			<option value="7">July</option>
			<option value="8">August</option>
			<option value="9">September</option>
			<option value="10">October</option>
			<option value="11">November</option>
			<option value="12">December</option>
		</select>
		Year : <input type="text" name="cyear[]" maxlength="4" size="4" class="year">
		</td>
	</tr>
		<tr><td>Where do they live ?</td><td colspan="3"><input type="radio" name="living[]" id="living-me" class="living-me"/>With Me<input type="radio" name="living[]" id="living-other" class="living-other"/>With Other Parent<input type="radio" name="living[]" id="living-own" class="living-own"/>Own</td></tr>
		<tr><td>House Address</td></tr>
		<tr><td>Line 1  :</td><td><input type="text" name="child-line1[]" id="child-line1" size="20" class="style" /></td>
		<td>Mobile No :</td><td><input type="text" name="child-mobile[]" id="child-mobile" class="style" /></td></tr>
		<tr><td>Office No :</td><td><input type="text" name="child-office[]" id="child-office" class="style" /></td>
		<td>Email Address : </td><td><input type="email" name="child-email[]" id="email" class="style" /></td></tr>
	</tbody>
</table>
Link to comment
Share on other sites

You should add each child contact information as a separate record instead of trying to concatenate all of them into a single record.

 

You can't store an array - as an array - into the database. You are trying to store an array into a field meant to hold a string value. (There is no array type of DB field). You *could* concatenate all of the arrays into single values, but that's a bad idea. Process each record individually as stated above.

Edited by Psycho
Link to comment
Share on other sites

im not sure what splitted arrays look like. is it like this?

$cday = ($_POST['cday']);
$cmonth = ($_POST['cmonth']);
$cyear = ($_POST['cyear']);
$childsalutations = ($_POST['child-salutations']);
$childfname = ($_POST['child-fname']);
$childlname = ($_POST['child-lname']);
$childline1 = ($_POST['child-line1']);
$childemail = ($_POST['child-email']);
$childm = ($_POST['child-mobile']);
$childoff = ($_POST['child-office']);

$info = array('c_name' => $childsalutation $childfname $childlname, 'c_dob' => $cday-$cmonth-$cyear, 'c_line1' => $childline1, 'c_mobile' => $childm, 'c_office' => $childoff, 'c_email' => $childemail)
Link to comment
Share on other sites

OK, let's step back a moment. I would advise against using non-named indexes in your form field names when you have "grouped" form fields. The reason is that you can't reply upon the ones with the same index being from the same group. A checkbox field that is not checked is not sent in the post data. Even if you have no checkbox fields, it's just good practice.

 

I would suggest the following - create the 'groups' of fields as a multi-dimensionsal array. Make the first dimension the index of the group and the second dimension the field names. For example:

 

The first group would look like this (NOTE: ids MUST be unique):

<select name="contact[0][child-salutations" id="child-salutations_0">
<input type="text" name="contact[0][child-fname]" id="child-fname_0" class="style" />
<input type="text" name="contact[0][child-lname]" id="child-lname_0" class="style" />
<select name="contact[0][cday]">
<select name="contact[0][cmonth]">
<input name="contact[0][cyear]" type="text" maxlength="4" size="4" class="year">
<input name="contact[0][living]" type="radio" id="living-other_0" class="living-other"/>
<input name="contact[0][living]" type="radio" id="living-own_0" class="living-own"/>
<input name="contact[0][child-line1]" type="text" id="child-line1_0" size="20" class="style" />
<input name="contact[0][child-mobile]" type="text" id="child-mobile_0" class="style" />
<input name="contact[0][child-office]" type="text" id="child-office_0" class="style" />
<input name="contact[0][child-email]" type="email" id="email_0" class="style" />

The second set would look like this

 

<select name="contact[1][child-salutations" id="child-salutations_1">
<input type="text" name="contact[1][child-fname]" id="child-fname_1" class="style" />
<input type="text" name="contact[1][child-lname]" id="child-lname_1" class="style" />
<select name="contact[1][cday]">
<select name="contact[1][cmonth]">
<input name="contact[1][cyear]" type="text" maxlength="4" size="4" class="year">
<input name="contact[1][living]" type="radio" id="living-other_1" class="living-other"/>
<input name="contact[1][living]" type="radio" id="living-own_1" class="living-own"/>
<input name="contact[1][child-line1]" type="text" id="child-line1_1" size="20" class="style" />
<input name="contact[1][child-mobile]" type="text" id="child-mobile_1" class="style" />
<input name="contact[1][child-office]" type="text" id="child-office_1" class="style" />
<input name="contact[1][child-email]" type="email" id="email_1" class="style" />

 

You can then iterate through each group of fields like this

 

foreach($_POST['contact'] as $contact)
{
    //Process the contact record
}
Edited by Psycho
Link to comment
Share on other sites

i can't set it manually. the child form is cloned by using a loop based on values of a drop down:

 

script:

$(document).ready(function()
{
    $('select[name=numc]').change(function()
    {
        var number_of_forms_to_create = $(this).val();
        var table = $('table.prime').clone();
        var target = $('div#add-child-wrap');
        for(var i = 0; i < number_of_forms_to_create; i++)
        {
            var clone = table.clone();
            clone.attr('id','style', '');
            clone.prependTo(target);
        }
    });
});

i tried this:

<?php

$number_of_children = count($_POST['child-fname']; // Get the number of children to be added to the database

$count = 0;
$children=array(); // Create the array that will contain the rearranged data

while ( $count <> $number_of_children ) {
    $children[$count]['child-salutations']     = $_POST['child-salutations'][$count];
    $children[$count]['child-fname']         = $_POST['child-fname'][$count];
    $children[$count]['child-lname']         = $_POST['child-lname'][$count];
    $children[$count]['cday']         = $_POST['cday'][$count];
    $children[$count]['cmonth']         = $_POST['cmonth'][$count];
    $children[$count]['cyear']         = $_POST['cyear'][$count];
    
    // We set a default value here if no option has been selected due to the way PHP handles radio buttons and check-boxes where no option has been selected
    if (!isset($_POST['living'][$count])) {
        $children[$count]['living']  = 'living-me';
    } else {
        $children[$count]['living']  = $_POST['living'][$count];
    $children[$count]['child-line']         = $_POST['child-line'][$count];
    $children[$count]['child-mobile']         = $_POST['child-mobile'][$count];
    $children[$count]['child-office']         = $_POST['child-office'][$count];
    $children[$count]['child-email']         = $_POST['child-email'][$count];
    $count++
}

echo '<pre>';
var_dump($children);
echo '</pre>';

foreach ( $children AS $child ) {
    //do something here
}
?>

but i literally had thousands of the same name recorded into the database when i submitted the form to test it out.

Link to comment
Share on other sites

i can't set it manually. the child form is cloned by using a loop based on values of a drop down:

 

 . . . 

 

but i literally had thousands of the same name recorded into the database when i submitted the form to test it out.

 

I didn't state to set it manually. I only stated that you should use numerically based indexes to ensure the "sets" of fields are logically grouped. You can do that programatically. You have a lot of different things going on to make this functionality work and, sorry, but I'm not going to take the time to rewrite it all. Even trying to tell you how to do it would likely take even more time.

 

If you want to take the easy way out, keep your field names as you already have them (e.g. name="fieldname[]". There are downsides to that, but it *should* work with what you have now. Then you can process the form submissions like this:

 

 

foreach($_POST['child-salutations'] as $index => $salutation)
{
    //Get the other variables for this index
    $fname = $_POST['child-fname'][$index];
    $lname = $_POST['child-lname'][$index];
    $cday = $_POST['cday'][$index];
   // etc. for all other fields
 
   //Process the data for this record
}
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.