简体   繁体   English

如何将动态添加的内容存储到数据库

[英]How to store a dynamically added content to database

I am having a few trouble storing my content into the database. 将内容存储到数据库时遇到一些麻烦。 The table/rows are dynamically generated using jquery! 该表/行是使用jquery动态生成的! but when i click the submit button, just a row of numbers is stored in my database table even when i store text values. 但是,当我单击提交按钮时,即使我存储文本值,数据库表中也只会存储一行数字。 Here is my jquery code: 这是我的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>/>";
}

Here is my html: 这是我的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>

and finally my php code: 最后是我的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");
}
}
}

Thanks for the help 谢谢您的帮助

If you mean you only get the last filled data from each different fields that happens because your fields name is not a array, change the names adding on end [] 如果您的意思是由于字段名称不是数组,那么只能从每个不同的字段中获取最后填充的数据,请更改名称并添加到end []

Like that: 像那样:

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

And the same for the others fields. 其他字段也一样。 Also you can optimize your php code a little bit more like that bellow. 您也可以像下面这样优化您的php代码。 I don't have your code but you can use var_dump($_POST) to check and correct. 我没有您的代码,但是您可以使用var_dump($ _ POST)进行检查和更正。

code updated 代码已更新

<?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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM