通过 PHP 从 HTML 表单插入到数据库中多个选项
2022-08-31 00:58:59
我有下拉列表,选择任何值后,它添加了带有一些字段的新行,示例如下图所示:
我有插入.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来检查表单的这一部分。你有没有想法如何解决它?