我创建了复杂的表单,我将尝试提供简化的示例。点击+
按钮可以生成更多字段。
例如,在表单中是字段:
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ] +
点击+
按钮添加重复行(通过javascript),点击+
按钮后,部分表单如下:
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ]
Certificate Date Of Issue Date of Expire
[ ] [ ] [ ] +
可以根据用户需要多次点击+
按钮。
在数据库表CV_Certificates
中,我有列CertificateId
,CertDateofIssue
,CertDateOfExpire
我正在使用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>
的Javascript
$(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);
}
答案 0 :(得分:1)
每个输入添加必须具有新名称,或者变量必须是数组:
<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);
}
}