如何使用PHP和MySQL从单个表单一次插入多个值

时间:2018-12-08 20:18:56

标签: php mysql forms post

我试图使用php在mysql中插入多个名称及其相应的姓氏,但似乎出了点问题。我尝试了google和这个网站,但是我还没有最好的答案来满足我的要求。我也设法插入了它们,但是姓氏可能使用了错误的用户名,然后我决定删除该代码,然后开始一个新的代码。任何帮助将不胜感激。以下是我尝试过的。

 <form action ="dependant_Process_Trevor.php?memberid=<?php echo $_GET['memberid']; ?>" name="contactform"  method="post" id ="---contactform" >
               <input type="hidden" name ="memberid" value="<?php echo $_GET['memberid']; ?>" >
                <div class="box-header">
                </div>
            <!-- Start Trevor -->
              <div class="box box-danger">
                <div class="box-header with-border">
                  <h3 class="box-title">Dependents / Children under the age of 18 years - maximum 6 children</h3>
                </div>
                    <!-- First child -->
                <div class="box-body">
                  <div class="row">
                  <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                      <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->

                    <!-- Start second child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                     <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->
                    <!-- Third  child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                     <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End third   child -->

                <!-- Fourth child -->
                <div class="box-body">
                  <div class="row">
                  <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                       <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End second  child -->

                    <!-- Fifth child -->
                <div class="box-body">
                  <div class="row">
                   <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                       <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>
                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End fifth  child -->
                    <!-- six  child -->
                <div class="box-body">
                  <div class="row">
                    <div class="col-xs-3">
                      <input type="text" name ="name[]"class="form-control" placeholder="Name">
                    </div>
                    <div class="col-xs-4">
                      <input type="text" name ="surname[]" class="form-control" placeholder="Surname">
                    </div>
                    <div class="col-xs-5">
                    <label>parent_type</label>
                   <select  name ="parent_type[]" "> 
                       <option  value ="" >Select Type</option>
                      <option name ="parent_type[]" value ="Son">Son</option>
                      <option name ="parent_type[]" value ="Daughter">Daughter</option>
                      <option name ="parent_type[]" value ="Brother">Brother</option>
                      <option name ="parent_type[]" value ="Grandchild">Grandchild</option>
                      <option name ="parent_type[]" value ="Nephew">Nephew</option>
                      <option name ="parent_type[]" value ="Niece">Niece</option>

                    </select>
                    </div>
                  </div>
                </div><!-- /.box-body -->
                <!-- End six  child -->

              </div><!-- /.box -->
              <!-- End  Trevor -->  

                <div class="box-body">
                  <!-- Date dd/mm/yyyy -->
                  <div class="form-group">
                    <div class="input-group">
                      <div class="input-group-addon">
                      </div>
                    </div><!-- /.input group -->
                  </div><!-- /.form group -->

<input type="submit" name ="submit" value ="Save Data" class="btn-primary">
</form>

下面是我的php

//$surname =(isset($_POST['surname']))? trim ($_POST['surname']):'';
$recieved_by =($_SESSION['username']);
$othername =(isset($_POST['othername']))? trim ($_POST['othername']):'';
$gender =(isset($_POST['gender']))? trim ($_POST['gender']):'';
//$parent_type =(isset($_POST['parent_type']))? trim ($_POST['parent_type']):'';
$id_passport =(isset($_POST['id_passport']))? trim ($_POST['id_passport']):'';
$tel =(isset($_POST['tel']))? trim ($_POST['tel']):'';
$work_contact =(isset($_POST['work_contact']))? trim ($_POST['work_contact']):'';
$address =(isset($_POST['address']))? trim ($_POST['address']):'';
$work_address =(isset($_POST['work_address']))? trim ($_POST['work_address']):'';
$comments =(isset($_POST['comments']))? trim ($_POST['comments']):'';
$cell =(isset($_POST['cell']))? trim ($_POST['cell']):'';
//cell convert to 27 


if(isset($_POST['submit'])){
foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )


$count = count($_POST['name']);

if(is_array($name) && is_array($surname) && is_array($parent_type)) {

    for($i = 0; $i < $count; $i++) {


//$name =  $_POST['name'][$i], $_POST['surname'][$i] , $_POST['parent_type'][$i];
//echo $name; 
///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport, 
cell, tel, work_contact, address, work_address, comments, 
memberid,companyid, joined_date, usercode, recieved_by , deadalive
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport, 
:cell, :tel, :work_contact, :address, :work_address, :comments, 
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name,
':surname'=>$surname,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}

if($query){
echo '<h1 style="color:green">Dependant Created</h1>';
}

2 个答案:

答案 0 :(得分:1)

我认为这里有一些问题,首先是大部分代码都是多余的...

foreach($_POST['name'] as $name)
foreach ($_POST['surname'] as $surname)
foreach($_POST['parent_type'] as $parent_type )


$count = count($_POST['name']);

由于foreach()没有{},因此所有循环最终都将只执行最后一行,这将始终是相同的。因此,删除3个foreach()循环,然后开始

$count = count($_POST['name']);

然后为每个循环设置值,您几乎已经拥有(我认为)并且需要...

$name =  $_POST['name'][$i];
$surname = $_POST['surname'][$i];

最后进行测试以查看其是否有效

if($query){

不正确,您应该检查所有

$insert->execute(array(...)); 

语句返回true,这应表明它们已全部创建。

答案 1 :(得分:0)

最后我在@Nigel Ren的帮助下得到了答案,一切都很好,这里是答案。

if(isset($_POST['submit'])){
$surname = $_POST['surname'];
$name = $_POST['name'] ;
$parent_type = $_POST['parent_type'];
$count = count($_POST['name']);

if(is_array($name) && is_array($surname) && is_array($parent_type)) {
    for($i = 0; $i < $count; $i++) {
     var_dump($name[$i]);

$surname1 = $surname[$i];
$name1 =$name[$i];
$parent_type1 = $parent_type[$i];

///
#########################################################
$query ='INSERT INTO dependants(
name, surname, othername, gender, id_passport, 
cell, tel, work_contact, address, work_address, comments, 
memberid,companyid, joined_date, usercode, recieved_by , deadalive,parent_type
)
VALUES
(
:name, :surname, :othername, :gender, :id_passport, 
:cell, :tel, :work_contact, :address, :work_address, :comments, 
:memberid,:companyid, :joined_date, :usercode, :recieved_by ,:deadalive,:parent_type
)';
$insert=$con->prepare($query);
$insert->execute(array(
':name'=>$name1,
':surname'=>$surname1,
':othername'=>$othername,
':gender'=>$gender,
':id_passport'=>$id_passport,
':cell'=>$realcell,
':tel'=>$tel,
':parent_type'=>$parent_type1,
':work_contact'=>$work_contact,
':address'=>$address,
':work_address'=>$work_address,
':comments'=>$comments,
':memberid'=>$memberid,
':usercode'=>$usercode,
':joined_date'=>$joined_date,
':companyid'=>$companyid,
':recieved_by'=>$recieved_by,
':deadalive'=>$deadalive
));
}}}// all end