1

我在将内容存储到数据库中时遇到了一些问题。表/行是使用 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");
}
}
}

谢谢您的帮助

4

1 回答 1

1

如果您的意思是您只从每个不同的字段中获取最后填充的数据,因为您的字段名称不是数组,请更改在 end []上添加的名称

像那样:

<input type='text' name='Obstacle[]'>

其他领域也是如此。你也可以像下面这样优化你的 php 代码。我没有您的代码,但您可以使用 var_dump($_POST) 进行检查和更正。

代码更新

<?php
//simulating data from form
$_POST = array(
               'Obstacle' => array('ob1', 'ob2', 'ob3'),
               'Likelihood' => array('l1', 'l2', 'l3'),
               'Severity' => array('s1', 's2', 's3'),
               'Priority' => array('p1', 'p2', 'p3'),
               );
$user = 1;

//the code
 $sql = "INSERT INTO obstacles (ObstacleDescription, Priority, Uncertainty, Severity, UserName, ComplianceID, Accepted)
    VALUES (:ID, :obstacleDec, :priorityScale, :likelihoodScale, :severityScale, :aUser, :compID, :accepted)";
    // $stmt = $db->prepare($sql);

    for ($i=0; $i<count($_POST['Obstacle']); $i++) {
        $values = array(':obstacleDec'=>$_POST['Obstacle'][$i],':likelihoodScale'=>$_POST['Likelihood'][$i], ':severityScale'=>$_POST['Severity'][$i],':priorityScale'=>$_POST['Priority'][$i], ':aUser'=>$user, ':compID'=>'', ':accepted'=>'' );
        // var_dump($values);
        try{
            $result= $stmt->execute($values);
        }
        catch(PDOException $ex){
            die("Failed to run query: " . $ex->getMessage());
        }
    }

    header("Location: obstacles.php");
    die("Redirecting to obstacles.php");
    //the ifs you have here are not necessary, your code will never get here
于 2013-11-07T19:37:58.673 回答