通过 PHP 从 HTML 表单插入到数据库中多个选项

2022-08-31 00:58:59

我有下拉列表,选择任何值后,它添加了带有一些字段的新行,示例如下图所示:

drop-down list

我有插入.php将值插入MySQL数据库。但存在一个问题,即只有第一行的值才会插入到数据库中。

PHP 看起来像这样:

foreach($_POST['CertificateType'] as $key => $val){ 

    $CertificateType = $val;

    $CertificateType = $_POST['CertificateType'][$key]; 
    $STCWCode        = $_POST['STCWCode'][$key];            
    $CertNo          = $_POST['CertNo'][$key];          
    $FromCert        = $_POST['FromCert'][$key];            
    $ToCert          = $_POST['ToCert'][$key];  

    $CertificateType = mysqli_real_escape_string($link, $CertificateType);  
    $STCWCode        = mysqli_real_escape_string($link, $STCWCode);                 
    $CertNo          = mysqli_real_escape_string($link, $CertNo);           
    $FromCert        = mysqli_real_escape_string($link, $FromCert);                 
    $ToCert          = mysqli_real_escape_string($link, $ToCert);      

    $sql3 = "INSERT INTO Tbl (
        CertificateType     
        ,UserId    
        ,STCWCode               
        ,CertNo                 
        ,FromCert               
        ,ToCert
        ,DateCreated
    ) VALUES (
        '$CertificateType',
        '$UserID',  
        '$STCWCode',            
        '$CertNo',          
        '$FromCert',            
        '$ToCert',
        now())";
    if(mysqli_query($link, $sql3)){
        echo "Resume created successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
} 

HTML 看起来像这样:

<fieldset class="fieldset-borders">
    <legend>4. Licenses & Certificates</legend>
    <ul class="header"> 
      <li>
        <select id='options' name="CertificateType[]" class="field-style div-format align-left">
          <option selected disabled value="0">Select certificates</option>
          <option value="1">One</option>
          <option value="2">Two</option>
          <option value="3">Three</option>  
        </select>  
      </li>
    </ul>  
    <ul class="cert" id="cert">     
      <li>
        <ul class="column">         
          <li><p class="test-label11">Name</p></li>                     
        </ul>
      </li>
      <li>
        <ul class="column">         
          <li><p class="test-label11">STCW Code</p></li>                        
        </ul>
      </li>
      <li>
        <ul class="column">         
          <li><p class="test-label11">Cert. No</p></li>                     
        </ul>
      </li>
      <li>
        <ul class="column">         
          <li><p class="test-label11">Place of Issue</p></li>                       
        </ul>
      </li>
      <li>
        <ul class="column">         
          <li><p class="test-label11">Date of Issue</p></li>                        
        </ul>
      </li>
      <li>
        <ul class="column">         
          <li><p class="test-label11">Date of Expire</p></li>                       
        </ul>
      </li>
      </ul>
        <div class="action2" ></div>


</fieldset>

Javascript代码,你可以在JS FIDDLE上检查

我已经创建了JS FIDDLE来检查表单的这一部分。你有没有想法如何解决它?


答案 1

您需要使用此格式创建表单

<form action="insert.php" method="post">  
<ul>
    <li>
        <input name="CertificateType[0]" type="hidden">
        <input name="CertificateType[0]['STCWCode']" type="text">
        <input name="CertificateType[0]['CertNo']" type="text">
        <input name="CertificateType[0]['PlaceofIssueCert']" type="text">
        <input name="CertificateType[0]['FromCert']" type="date">
        <input name="CertificateType[0]['ToCert']" type="date">
    </li>
    <li>
        <input name="CertificateType[1]" type="hidden">
        <input name="CertificateType[1]['STCWCode']" type="text">
        <input name="CertificateType[1]['CertNo']" type="text">
        <input name="CertificateType[1]['PlaceofIssueCert']" type="text">
        <input name="CertificateType[1]['FromCert']" type="date">
        <input name="CertificateType[1]['ToCert']" type="date">
    </li>
    ...
</ul>
</form>

当您使用此格式提交表单时,您将在插入中收到数字键数组.php$_POST['CertificateType']

array(
  "0" => array(
           "STCWCode" => "somevalue",
           "CertNo" => "somevalue",
           "PlaceofIssueCert" => "somevalue",
           "FromCert" => "somevalue",
           "ToCert" => "somevalue",  
         ),
  "1" => array(
           "STCWCode" => "somevalue",
           "CertNo" => "somevalue",
           "PlaceofIssueCert" => "somevalue",
           "FromCert" => "somevalue",
           "ToCert" => "somevalue",  
         ),
   . . .
)

在这里,每个索引将表示一行。这可以使用 foreach 循环进行检索,如下所示:

更新您的 php 代码

foreach($_POST['CertificateType'] as $val){ 

$CertificateType = $val;

$CertificateType = $val; 
$STCWCode        = $val['STCWCode'];            
$CertNo          = $val['CertNo'];          
$FromCert        = $val['FromCert'];            
$ToCert          = $val['ToCert'];  

$CertificateType = mysqli_real_escape_string($link, $CertificateType);  
$STCWCode        = mysqli_real_escape_string($link, $STCWCode);                 
$CertNo          = mysqli_real_escape_string($link, $CertNo);           
$FromCert        = mysqli_real_escape_string($link, $FromCert);                 
$ToCert          = mysqli_real_escape_string($link, $ToCert);      

$sql3 = "INSERT INTO Tbl (
    CertificateType     
    ,UserId    
    ,STCWCode               
    ,CertNo                 
    ,FromCert               
    ,ToCert
    ,DateCreated
) VALUES (
    '$CertificateType',
    '$UserID',  
    '$STCWCode',            
    '$CertNo',          
    '$FromCert',            
    '$ToCert',
    now())";
if(mysqli_query($link, $sql3)){
    echo "Resume created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
} 

答案 2

使用您的小提琴JavaScript并在每行的隐藏字段中添加证书类型;并将其命名为RowCertificateType,与行的其他字段相同;

<input type="hidden" name="RowCertificateType[]" value="" />
<!-- set value of this field same as you are showing in li as label for this row; -->

然后在你的php脚本中使用如下:

foreach($_POST['RowCertificateType'] as $key=> $val){

  $CertificateType = $val;

  $STCWCode = $_POST['STCWCode'][$key];
  $CertNo = $_POST['CertNo'][$key];
  $FromCert = $_POST['FromCert'][$key];
  $ToCert = $_POST['ToCert'][$key];

  $CertificateType = mysqli_real_escape_string($link, $CertificateType);
  $STCWCode = mysqli_real_escape_string($link, $STCWCode);
  $CertNo = mysqli_real_escape_string($link, $CertNo);
  $FromCert = mysqli_real_escape_string($link, $FromCert);
  $ToCert = mysqli_real_escape_string($link, $ToCert);

  $sql3 = "INSERT INTO Tbl (
        CertificateType     
        ,UserId    
        ,STCWCode               
        ,CertNo                 
        ,FromCert               
        ,ToCert
        ,DateCreated
    ) VALUES (
        '$CertificateType',
        '$UserID',  
        '$STCWCode',            
        '$CertNo',          
        '$FromCert',            
        '$ToCert',
        now())";
  if(mysqli_query($link, $sql3)){
    echo "Resume created successfully.";
  }else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
  }
}     

:)