将多个值插入 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
我有列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>
爪哇语
$(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);
}
}
相关文章:
- 使用node-js上传文件,并将表单信息存储在数据库(mysql)中
- 缺少用于创建的模板.内部服务器错误-聚合物表单保存到数据库
- 表单将数据提交到数据库和url
- javascript验证数据库中的表单值
- 如何不允许在使用PHP的自动完成表单中不在数据库中的值&JS
- 如何将动态表单保存到数据库/编辑回表单
- 表单提交后保留数据库查询结果
- 当使用AJAX获取PHP文件以从MySQL数据库中检索数据时,会出现下拉表单disapering
- 通过ajax wordpress将表单值保存到数据库中
- 选择表单后从数据库中获取隐藏的输入值
- 我想显示数据库中的数据.如果我们新添加一个表单数据,我想附加显示的新帖子而不使用代码点火器刷新
- 我刚刚开始使用 MEAN 堆栈.我试图将我的表单提交数据存储到 mongo 数据库,但我无法这样做
- 动态表单无法正确插入到 MySQL 数据库中
- 如何使用 html 表单中的一个文本字段来搜索数据库表中的任何列
- 单击按钮时更改数据库表的字段值
- 如何在 html5 sql 数据库中存储表单信息
- 将多个值插入 HTML 表单中的同一数据库列
- 填写表单文本框,其中包含数据库表中的数据(如果用户存在)
- 如何从我的 MySQL 数据库中提取数据以在页面加载时预填充表单
- 如何将 XML 文件添加到 eXist 数据库表单 java 脚本