我有一个html网页,其中有一个表格,可以从我的数据库中的表中获取数据。每行都有一个按钮,点击后可以使用javascript动态添加到网页上的另一个表中。动态表有一个提交按钮,单击该按钮时,表中的数据应添加到我的数据库中的表中。我的问题是表中的内容没有添加到数据库中,但每次按下提交按钮时,都会添加一个具有唯一ID的新行。还有一个输入文本框,也可以正确添加到我的数据库中。这让我相信我的数据库连接正在工作,但由于某种原因,表中的数据不会被添加。如何使动态表中的数据进入数据库?这是我的代码。代码已经更新了建议。
<?php
$sql = "SELECT *
FROM inventoryCars";
$result = mysqli_query($connection, $sql);
?>
<div class="inventory">
<div class ="cars">
<table>
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$RegNo = $row["regNo"];
$CarMake = $row["carMake"];
$CarModel = $row["carModel"];
?>
<tr>
<input type="hidden" name="regNo" value="<?php echo $row['regNo'] ?>">
<input type="hidden" name="carMake" value="<?php echo $row['carMake'] ?>">
<input type="hidden" name="carModel" value="<?php echo $row['carModel'] ?>">
<td><?php echo $row["regNo"] ?></td>
<td><?php echo $row["carMake"] ?></td>
<td><?php echo $row["carModel"] ?></td>
<td><button onclick="addRowToTable('<?php echo $RegNo ?>','<?php echo $CarMake ?>','<?php echo $CarModel ?>')">+</button></td>
</tr>
<script type ="text/javascript">
function addRowToTable(regNo, carMake, carModel) {
var table = document.getElementById("newhireTBL");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = regNo;
cell2.innerHTML = carMake;
cell3.innterHTML = carModel;
}
</script>
<?php
}
}
?>
</table>
</div>
</div>
<div class="order">
<div class ="newhire">
<table id="newhireTBL">
</table>
<form action = "addNewHire.php" method = "post">
<input type="hidden" name="regNo" value="<?php echo $row['regNo'] ?>">
<input type="hidden" name="carMake" value="<?php echo $row['carMake'] ?>">
<input type="hidden" name="carModel" value="<?php echo $row['carModel'] ?>">
<input id="submit" type="submit" name="submit" value="Submit">
</form>
</div>
</div>
addNewHire.php
<?php session_start(); ?>
<?php require_once("connectToSQL.php"); ?>
<?php
echo "<pre>";var_dump($_POST);echo "</pre>";die;
if (isset($_POST['submit'])){
$RegNo = $_POST['regNo'];
$CarMake = $_POST['carMake'];
$CarModel = $_POST['carModel'];
$_SESSION["regNo"] = $RegNo;
$_SESSION["carMake"] = $CarMake;
$_SESSION["carModel"] = $CarModel;
$sql = "INSERT INTO newHire(regNo, carMake, carModel)
VALUES('$RegNo', '$CarMake', '$CarModel')";
if (mysqli_query($connection, $sql)) {
header("location:order_success.php");
echo "Order has been made";
} else {
header("location:order_fail");
echo "Order fail";
}
}
?>
}
?>
测试显示了这个
array(4) {
["regNo"]=>
string(0) ""
["carMake"]=>
string(0) ""
["carModel"]=>
string(0) ""
["submit"]=>
string(6) "Submit"
}
答案 0 :(得分:0)
表单和表格标签的嵌套无效,这可能会阻止提交表单数据。尝试为&#34; newhireTBL&#34;移动结束标记。表位于表格的开始标记(表格外)的正下方,因此表格和表格标签不会重叠。
例如:
<table...>
</table>
<form...>
</form>
编辑:这是一个基本示例,其中包含一些可以帮助您解决问题的虚拟数据。祝你好运!
<?php
$rows = array(
array(
"regNo" => "abc123",
"carMake" => "toyota",
"carModel" => "camry",
),
array(
"regNo" => "def456",
"carMake" => "ford",
"carModel" => "laser",
),
);
?>
<?php foreach ($rows as $row): ?>
<?php echo $row["regNo"] ?>
<?php echo $row["carMake"] ?>
<?php echo $row["carModel"] ?>
<button onclick="addRow('<?php echo $row["regNo"] ?>','<?php echo $row["carMake"] ?>','<?php echo $row["carModel"] ?>')">+</button>
<br/>
<?php endforeach ?>
<form action = "addNewHire.php" method = "post">
<input id=regNo type="text" name="regNo" value="">
<input id=carMake type="text" name="carMake" value="">
<input id=carModel type="text" name="carModel" value="">
<input id="submit" type="submit" name="submit" value="Submit">
</form>
<script type ="text/javascript">
function addRow(regNo, carMake, carModel) {
document.getElementById('regNo').value = regNo;
document.getElementById('carMake').value = carMake;
document.getElementById('carModel').value = carModel;
}
</script>