在Mysql和php中使用foreach显示两个数组的值并将其保存在数据库中(Display the values of two arrays and save it in the database

编程入门 行业动态 更新时间:2024-10-25 08:21:54
在Mysql和php中使用foreach显示两个数组的值并将其保存在数据库中(Display the values of two arrays and save it in the database using foreach in Mysql and 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保存它?

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

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?

最满意答案

希望这会起作用

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

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

更多推荐

本文发布于:2023-08-08 00:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   数据库中   两个   并将其   foreach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!