hi guys so this page where part of the form elements are made arrays by adding square brackets to the input names.
Line 1 :<input type="text" name="child-line1[]" id="child-line1"/>
Mobile No :<input type="text" name="child-mobile[]" id="child-mobile"/>
Line 2 :<input type="text" name="child-line2[]" id="child-line2" />
Office No :<input type="text" name="child-office[]" id="child-office" />
Email Address :<input type="email" name="child-email[]" id="email" />
i got it inserted into my database but in the specified field it says array.
|child_name|dob|house_add1|house_add2|mobile|office|email|
inserted:
|array|array-array-array|array| |array|array|array|
query:
$add5 = mysql_query("INSERT INTO child VALUES('','".$childsalutations." $childfname $childlname',' ".$cday."-".$cmonth."-$cyear ','$childline1','$childline2','$childm','$childoff','$childemail')") or die(mysql_error());
why?
i used to check:
print_r ($_POST['child-fname']);
print_r ($_POST['child-lname']);
print_r ($_POST['child-line1']);
print_r ($_POST['child-line2']);
print_r ($_POST['child-mobile']);
print_r ($_POST['child-office']);
print_r ($_POST['cday']);
print_r ($_POST['cmonth']);
print_r ($_POST['cyear']);
result(which appears on top of page after insert):
Array ( [0] => Alia [1] => Ali ) Array ( [0] => Razali [1] => Razali ) Array ( [0] => house 2 [1] => house 1 ) Array ( [0] => [1] => ) Array ( [0] => 014965872 [1] => 0139874562 ) Array ( [0] => 79563214 [1] => - ) Array ( [0] => 16 [1] => 12 ) Array ( [0] => 4 [1] => 10 ) Array ( [0] => 1993 [1] => 1998 )
so it does read just wont submit as it is.
thanks in advance!
What are you expecting to happen? Do you want it to create two rows, one for each child? It seems as if what you’re doing here is asking mysql to create a new row, then for each column (which is probably defined as a string type) you’re passing an array of more than one string. I would probably have a loop for each value in the array, and build a separate query to insert a new row for each one, rather than passing the array as a whole. Maybe someone else can say whether that’s possible or not.
When you access the submitted data you’ll get a multidimensional array, each sub-array will be an array of all the values submitted for a single field. You’ll need to either work your way through each sub-array simultaneously or re-arrange the multidimensional array so that each sub-array relates to just one child.
Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.
Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.
this is what my form looks like:
as you can c users if select radio button yes at “Children ?” then a drop down will be enabled.
then say if number of children is 2 then two forms will appear for the childs detail.
so yes, if there is two or more i would like it that new rows are added accordingly in the database.
um yeah about the mysql_ im working on that. probably be working on mysqli* for now, especially for this project because im in a jam. so yeah.
thanks.
What’s the table structures for the tables concerned (the output of a SHOW CREATE TABLE query will show the structure)?
What info from the form is supposed to go into what table?
Well, the first part of the form which is before “Children ?” when inserted goes into table in database called contact. the info for the child(salutation,first name, last name, etc) goes into child table. info that goes into contact is inserted as it should just for child im having issues coz its array.
child:
child_id|c_name|c_dob|c_line1|c_mobile|c_office|c_email|
auto-incremented|salutation first name last name (string)|DOB (string)|line 1 (string)|mobile no (string)|office no (string)|email add (string)|
in a stackoverflow question someone says: information stating array needs 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)
Can you please post the HTML for the “child” part of the form?
its a bit 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>
<?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
}
?>
That will rearrange the “child” data so that each subarray is all the details of one child instead of each of the subarrays being one field. Inside the foreach loop you would do your validation for each child, eg:
- Are all fields filled in that are meant to be filled in.
- Are all fields within the range of lengths that you expect
- etc
Once you’ve validated your data, in the same foreach loop you’d insert the data into the database, remembering to escape all data. When you migrate to PDO you’ll be able to make use of prepared statements which are a far superior way of preventing SQL injection attacks.
Note: The above code has not been tested.
thanks spacephoenix. i will try that out and if i face any probs ill post.
in the foreach loop do i add:
$childfname = mysqli_real_escape_string($con, $_POST['child-salutations'][$count]);
? just wondering if i should or shouldnt.
also in the while loop these:
$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];
don’t need to be added?
omg the database gave me a shock. i implemented the coding that u gave me and i literally had thousands of the same record of one child.