【问题标题】:How To Insert multiple values from a single form at once using PHP and MySQL如何使用 PHP 和 MySQL 一次从一个表单中插入多个值
【发布时间】:2018-12-08 20:18:55
【问题描述】:

我尝试使用 php 在 mysql 中插入多个名称及其对应的姓氏,但似乎有什么地方做错了。我尝试了谷歌和这个网站,但我还没有得到满足我要求的最佳答案。我也设法插入它们,但姓氏可能会转到错误的用户名,然后我决定删除该代码并开始一个新的。任何帮助将不胜感激。以下是我尝试过的。

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

【问题讨论】:

    标签: php mysql forms post


    【解决方案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,这应该表明它们都已被创建。

    【讨论】:

    • 您能否将整个代码编译为单个单元而不是部分部分。感谢您的努力,先生,非常感谢
    • 没什么大不了的,我原以为能够进行少量更改比大量代码要容易得多。
    • 我进行了更改,它插入了数组文本,它给了我这个错误:Array to string conversion in .我认为这是我现在的问题,因为你设法让我找到了解决方案。
    • 一件事是,如果您可以识别错误所在的行,则更容易找出导致错误的原因 - 如果您需要错误报告 - stackoverflow.com/questions/1053424/…
    • 我认为我们现在必须将数组转换为实名而不是在bd中发布单词Array
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多