简体   繁体   中英

Display the values of two arrays and save it in the database using foreach in Mysql and php

I have two arrays, the array that holds the child name and the other one is an array that holds the birthday of the child. This is the design of my form that the user will fill up. 在此处输入图片说明

 <div id="Children"> <div class="form-group col-md-7"> <label for="child">Name of Child</label> <input type="text" class="form-control" name="child[]" id="child" placeholder="FULL NAME"> </div> <div class="form-group col-md-5"> <label for="ch_DateOfBirth">Date of Birth</label> <input type="text" class="form-control date-picker" name="ch_DateOfBirth[]" id="DateOfBirth" placeholder="Date of Birth"> </div> 

and this is my php code for saving the two arrays in the database.

 $child_name=$_POST['child']; $child_bday=date('Yd-m', strtotime($_POST['ch_DateOfBirth'])); $count=count($child_name); for ($i=0; $i < $count ; $i++) { $sql6="INSERT into tbl_children (Emp_ID, Ch_Name, Ch_Bdate) values ('".$emp_id."', '".$child_name[$i]."', '".$child_bday[$i]."') "; $dbcon->query($sql6); } 

but whenever i tried to do so, only the name of the children are saved excluding their birthdays. How can I save it using foreach if possible?

在此处输入图片说明

Hope this will works

$child_name=$_POST['child'];
$count=count($child_name);

for ($i=0; $i < $count ; $i++) {
  $child_bday=date('Y-d-m', strtotime($_POST['ch_DateOfBirth'][$i]));
  $sql6="INSERT into  tbl_children (Emp_ID, Ch_Name, Ch_Bdate) values ('".$emp_id."', '".$child_name[$i]."', '".$child_bday."') ";
  $dbcon->query($sql6);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM