从表单插入多个列到mysql

时间:2014-03-27 16:10:09

标签: php

这是我的html表单


<div id="customcopies">
    <table>
        <tr>
            <th>Member Name:</th>
            <th>Relation with head of the family:</th>
            <th>Date of Birth:</th>
            <th>Blood Group:</th>
            <th>Education:</th>
            <th>Business / Employee:</th>
            <th>Married / Un-Married:</th>
        </tr>
        <tr>
            <td><input type="text" class="form-control" id="m_name_1" name="m_name[]"/></td>
            <td><input type="text" class="form-control" id="m_relation_1" name="m_relation[]"/></td>
            <td><input type="date" class="form-control" id="m_dob_1" name="m_dob[]"></td>
            <td><input type="text" class="form-control" id="m_bgroup_1" name="m_bgroup[]"/></td>
            <td><input type="text" class="form-control" id="m_education_1" name="m_education[]"/></td>
            <td><input type="text" class="form-control" id="m_business_1" name="m_business[]"/></td>
            <td><input type="text" class="form-control" id="m_married_1" name="m_married[]"/></td>
        </tr>
    </table>
</div>

insert.php


$insert1 =$db->prepare("INSERT INTO members (user_id, m_name, m_relation, m_dob, m_bgroup, m_education, m_business, m_married)VALUES (:us, :m_name, :m_relation, :m_dob, :m_bgroup, :m_education, :m_business, :m_married)");

$insert1->bindParam(":us", $us);
$insert1->bindParam(":m_name", $m_name);
$insert1->bindParam(":m_relation", $m_relation);
$insert1->bindParam(":m_dob", $m_dob);
$insert1->bindParam(":m_bgroup", $m_bgroup);
$insert1->bindParam(":m_education", $m_education);
$insert1->bindParam(":m_business", $m_business);
$insert1->bindParam(":m_married", $m_married);
$insert1->execute();

我有一个jquery代码,通过它我可以多次添加相同的表...在填写表单后,最后一列只会插入到数据库中 例如: 第一名成员详情 第二名成员详情 第3名成员详情

只有第3个成员的详细信息....正在插入

1 个答案:

答案 0 :(得分:0)

您需要在表单周围添加表单标记才能将其发送到insert.php。我不确定你为什么要输入输入数组的名字。你有相同输入名称的倍数?如果你有多个同名并且有一个数组,那么你需要使用for循环来遍历每个数组并将它们插入到数据库中。如果每个输入数组中的值都相同,那么它就会更容易。

<div id="customcopies">
<form action='insert.php' method='post'>
  <table>
   <tr>
     <th>Member Name:</th>
     <th>Relation with head of the family:</th>
     <th>Date of Birth:</th>
     <th>Blood Group:</th>
     <th>Education:</th>
     <th>Business / Employee:</th>
     <th>Married / Un-Married:</th>
   </tr>
   <tr>
     <td><input type="text" class="form-control" id="m_name_1" name="m_name[]"/></td>

     <td><input type="text" class="form-control" id="m_relation_1" name="m_relation[]"/></td>

     <td><input type="date" class="form-control" id="m_dob_1" name="m_dob[]"></td>

     <td><input type="text" class="form-control" id="m_bgroup_1" name="m_bgroup[]"/></td>

     <td><input type="text" class="form-control" id="m_education_1" name="m_education[]"/></td>

     <td><input type="text" class="form-control" id="m_business_1" name="m_business[]"/></td>

     <td><input type="text" class="form-control" id="m_married_1" name="m_married[]"/></td>
   </tr>                                                               
  </table>  
</form>  
</div>

现在我们需要将每个值插入数据库。我假设每个数组中有相同数量的值。我不知道$ us来自哪里?

$us=$_SESSION['userid']; // THIS IS ASSUMING HOW TO GET THE USERID B/C YOU DIDN'T SAY HOW.
$m_name=$_POST['m_name'];
$m_relation=$_POST['m_relation'];
$m_dob=$_POST['m_dob'];
$m_bgroup=$_POST['m_bgroup'];
$m_education=$_POST['m_education'];
$m_business=$_POST['m_business'];
$m_married=$_POST['m_married'];

$arrayCount=count($m_name); //THIS WILL TELL FOR LOOP HOW MANY TIMES TO LOOP.

for (i=1; i<=$arrayCount; i++) {
  $num=$i-1;

  $insert1 =$db->prepare("INSERT INTO members (user_id, m_name, m_relation, m_dob, m_bgroup, m_education, m_business, m_married)VALUES (:us, :m_name, :m_relation, :m_dob, :m_bgroup, :m_education, :m_business, :m_married)");

  $insert1->bindParam(":us", $us);
  $insert1->bindParam(":m_name", $m_name[$num]);
  $insert1->bindParam(":m_relation", $m_relation[$num]);
  $insert1->bindParam(":m_dob", $m_dob[$num]);
  $insert1->bindParam(":m_bgroup", $m_bgroup[$num]);
  $insert1->bindParam(":m_education", $m_education[$num]);
  $insert1->bindParam(":m_business", $m_business[$num]);
  $insert1->bindParam(":m_married", $m_married[$num]);
  $insert1->execute();
}

如果这不是您所说的话,请告诉我,我会根据您的需要对其进行编辑。