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

问题描述:

我有两个数组,其中包含子名称的数组,另一个是保存子的生日的数组。 这是我的表单的设计,用户将填满。 enter image description here显示两个数组的值,并将其保存在数据库中使用foreach在Mysql和php中

<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>

,这是用于保存两个数组在数据库中我的PHP代码。

$child_name=$_POST['child']; 
 
$child_bday=date('Y-d-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); 
 
}

但每当我试图这样做,只有孩子的名字都保存不包括他们的生日。如果可能,我如何使用foreach保存它?

enter image description here

+0

检查,看看阉了'$ child_bday'实际上包含的值,用'的print_r()'这个 –

+0

我尝试使用破灭(),它显示值。 – Laurie

+0

什么是JavaScript中的日期格式,何时发送。它将需要发送日期,而不是字符串? –

希望这将工作

$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); 
} 
+0

它的工作!谢谢! @SyamMohan – Laurie

+0

但我遇到了一个小问题,我的表单有一个功能,用户可以添加行,所以我尝试填充添加的行并跳过默认的前三行。当我保存它时,前三行保存一个默认的出生日期1970-01-01和一个空的子名称,然后添加我填满的行,保存子名称但空的出生日期。我使用devtools检查了元素,添加的行具有与原始行相同的名称属性。 – Laurie