简体   繁体   English

显示两个数组的值,并使用Mysql和php中的foreach将其保存在数据库中

[英]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. 这是我的PHP代码,用于将两个数组保存在数据库中。

 $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? 如果可能,如何使用foreach保存它?

在此处输入图片说明

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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