繁体   English   中英

从HTML表单向同一数据库列插入多个值

[英]Insert multiple values to the same database column from HTML form

我创建了复杂的表单,我将尝试提供简化的示例。 单击+按钮可以生成更多字段。

例如,形式是字段:

Certificate    Date Of Issue    Date of Expire   
[         ]    [           ]    [            ]   +

通过单击+按钮它添加重复的行(通过javascript),因此在单击+按钮后,表单的一部分看起来像:

Certificate    Date Of Issue    Date of Expire   
[         ]    [           ]    [            ]

Certificate    Date Of Issue    Date of Expire  
[         ]    [           ]    [            ]   +

可以按用户需要多次单击+按钮。

在数据库表CV_Certificates我具有CertificateIdCertDateofIssueCertDateOfExpire

我正在使用insert.php在以下位置将值插入数据库:

$Certificate = mysqli_real_escape_string($link, $_POST['Certificate']);             
$CertDateOfIssue = mysqli_real_escape_string($link, $_POST['CertDateOfIssue']);             
$CertDateOfExpire = mysqli_real_escape_string($link, $_POST['CertDateOfExpire']);

$sql = "INSERT INTO CV_Certificates (Certificate, CertDateOfIssue, CertDateOfExpire) VALUES ('$Certificate','$CertDateOfIssue','$CertDateOfExpire')

if(mysqli_query($link, $sql)){
    echo "Resume created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

它仅从1行中插入值,但应将所有添加的行中的值插入到多个数据库表的行中。


的HTML

<fieldset class="fieldset-borders">
<legend>5. Certificates</legend>

<ul class="Certificates" id="Certificates"> 
    <li>
        <ul class="column">         
            <li>
                <label for="Certificate">Certificate Name</label>
                <input id="Certificate" type="text" name="Certificate" class="field-style field-split25 align-left" placeholder="Certificate" />    
            </li>
        </ul>
    </li>   
    <li>
        <ul class="column">         
            <li>
                <label for="CertDateOfIssue">Date of Issue</label>
                <input id="CertDateOfIssue" type="date" name="CertDateOfIssue" class="field-style field-split25 align-left" placeholder="Date of Issue" />  
            </li>
        </ul>
    </li>
    <li>
        <ul class="column">         
            <li>
                <label for="CertDateOfExpire">Date of Expire</label>
                <input id="CertDateOfExpire" type="date" name="CertDateOfExpire" class="field-style field-split25 align-left" placeholder="Date of Expire" />   
            </li>
        </ul>
    </li>
</ul>   
<ul class="Certificates1"></ul>
<button type="button" class="add-row1">+</button> 

</fieldset>

Java脚本

$(document).ready(function(){
   $( ".add-row1" ).click(function(){
      $( "ul.Certificates" ).first().clone().appendTo( ".Certificates1" ).append('<button class="remove">X</button>').find('input').val("");
   });
   $( "body" ).on('click', '.remove', function(){
    $(this).closest('.Certificates').remove();
  });
});

我该如何实现? 另外,正如我注意到的那样,这是极其简化的形式,总共有150多个字段。

更新

这是我的代码中返回错误的真实部分: ERROR: Could not able to execute . 而且现在根本不将值插入数据库。

foreach($SeaService as $key=>$res) {

$sql2 = "INSERT INTO CV_SeaServices (
UserId
,NameOfVessel           
,TypeOfVessel           
,YearBuilt              
,Flag                   
,DWT                    
,TypeOfMEkW             
,SSRankApplied          
,SignOn                 
,SignOff                
,CompanyName            
) VALUES (
'$res',
'$NameOfVessel[$key]',      
'$TypeOfVessel[$key]',      
'$YearBuilt[$key]',             
'$Flag[$key]',              
'$DWT[$key]',               
'$TypeOfMEkW[$key]',        
'$SSRankApplied[$key]',         
'$SignOn[$key]',            
'$SignOff[$key]',           
'$CompanyName[$key]')";

};
if(mysqli_query($link, $sql2)){
    echo "Resume created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

每个输入的加法必须有一个新名称,或者变量必须是一个数组:

<input id="Certificate" type="date" name="Certificate[]" class="field-style field-split25 align-left" placeholder="Date of Expire" />`

<input id="CertDateOfIssue" type="date" name="CertDateOfIssue[]" class="field-style field-split25 align-left" placeholder="Date of Issue" /> `

<input id="CertDateOfIssue" type="date" name="CertDateOfIssue[]" class="field-style field-split25 align-left" placeholder="Date of Issue" />`

现在在您的PHP中

$certificate = mysqli_real_escape_string($link, $_POST['Certificate']);          
$certDateOfIssue = mysqli_real_escape_string($link, $_POST['CertDateOfIssue']);             
$certDateOfExpire = mysqli_real_escape_string($link, $_POST['CertDateOfExpire']);

foreach($certificate as $key=>$res) {
$sql = "INSERT INTO CV_Certificates (Certificate, CertDateOfIssue, CertDateOfExpire) VALUES ('$res','$certDateOfIssue[$key]','$certDateOfExpire[$key]')
}

更新

foreach($SeaService as $key=>$res) {

$sql2 = "INSERT INTO CV_SeaServices (
UserId
,NameOfVessel           
,TypeOfVessel           
,YearBuilt              
,Flag                   
,DWT                    
,TypeOfMEkW             
,SSRankApplied          
,SignOn                 
,SignOff                
,CompanyName            
) VALUES (
'$res',
'$NameOfVessel[$key]',      
'$TypeOfVessel[$key]',      
'$YearBuilt[$key]',             
'$Flag[$key]',              
'$DWT[$key]',               
'$TypeOfMEkW[$key]',        
'$SSRankApplied[$key]',         
'$SignOn[$key]',            
'$SignOff[$key]',           
'$CompanyName[$key]')";
 if(mysqli_query($link, $sql2)){
    echo "Resume created successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM