我在将内容存储到数据库中时遇到了一些问题。表/行是使用 jquery 动态生成的!但是当我单击提交按钮时,即使我存储文本值,我的数据库表中也只会存储一行数字。这是我的jQuery代码:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = "<input type='text' name='Obstacle'>";
var cell3 = row.insertCell(2);
cell3.innerHTML = "<select name='Likelihood'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
var cell4 = row.insertCell(3);
cell4.innerHTML = "<select name='Severity'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
var cell5 = row.insertCell(4);
cell5.innerHTML = "<select name='Priority'> <option value='1'>Low</option> <option value='2'>Medium</option><option value='3'>High</option>/>";
}
这是我的html:
<div class="compTable">
<form action="" method="post">
<TABLE width="425" border="1">
<thead>
<tr>
<th width="4"></th>
<th width="100">Obstacle</th>
<th width="50">Likelihood</th>
<th width="50">Severity</th>
<th width="50">Priotity</th>
</tr>
</thead>
<tbody id="dataTable">
</tbody>
</TABLE>
<INPUT type="submit" value="Save" name="submit" />
<INPUT type="button" value="Add Obstacle" onClick="addRow('dataTable')" />
<INPUT type="button" value="Delete Obstacle" onClick="deleteRow('dataTable')" />
</form>
</div>
最后是我的 php 代码:
if(isset($_POST['submit'])){
$user = $_SESSION['user']['FullName'];
$obstacleDec = isset($_POST["Obstacle"][$key]);
$likelihoodScale = isset($_POST["Likelihood"][$key]);
$severityScale = isset($_POST["Severity"][$key]);
$priorityScale = isset($_POST["Priority"]);
foreach ($_POST as $key => $value){
$sql = "INSERT INTO obstacles (ObstacleID, ObstacleDescription, Priority, Uncertainty, Severity, UserName, ComplianceID, Accepted)
VALUES (:ID, :obstacleDec, :priorityScale, :likelihoodScale, :severityScale, :aUser, :compID, :accepted)";
try{
$stmt = $db->prepare($sql);
$result= $stmt->execute(array(':ID'=>'', ':obstacleDec'=>$obstacleDec,':likelihoodScale'=>$likelihoodScale, ':severityScale'=>$severityScale,':priorityScale'=>$priorityScale, ':aUser'=>$user, ':compID'=>'', ':accepted'=>'' ));
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
header("Location: obstacles.php");
die("Redirecting to obstacles.php");
if(!$sql){
die("error inserting values");
}
else{
die("successfully inserted the values");
}
}
}
谢谢您的帮助