Javascript 将数据从动态表提交到数据库-PHP

Javascript 将数据从动态表提交到数据库-PHP,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我有一个html网页,它有一个表,可以从数据库中的表中获取数据。每一行都有一个按钮,单击该按钮时,将使用javascript动态地添加到网页上的另一个表中。动态表有一个提交按钮,单击该按钮时,表中的数据应添加到数据库中的表中。我的问题是,表中的内容没有添加到数据库中,但每次按下submit按钮时,都会添加一个具有唯一id的新行。还有一个输入文本框,它也被正确地添加到我的数据库中。这使我相信我的数据库连接正在工作,但由于某种原因,表中的数据没有被添加。如何将动态表中的数据提交到数据库中?这是我的密

我有一个html网页,它有一个表,可以从数据库中的表中获取数据。每一行都有一个按钮,单击该按钮时,将使用javascript动态地添加到网页上的另一个表中。动态表有一个提交按钮,单击该按钮时,表中的数据应添加到数据库中的表中。我的问题是,表中的内容没有添加到数据库中,但每次按下submit按钮时,都会添加一个具有唯一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>

表单和表标记的嵌套无效,这可能会阻止提交表单数据。尝试将“newhireTBL”表的结束标记移动到表的开始标记的正下方(表单外部),这样表和表单标记就不会重叠

例如:


编辑:下面是一个基本示例,其中包含一些虚拟数据,可以帮助您解决问题。祝你好运



在您的
if条件
之前,在文件
addNewHire.php
中添加
echo';变量转储($\u POST);回声“;死亡并检查输出。使用
var\u dump
可以显示内容,使用
pre
标记可以以更可读的方式格式化输出。使用
die
您只需停止程序。您好@AMartinNo1这是输出,我认为没有向变量添加任何内容对吗<代码>数组(4){[“regNo”]=>string(0)”“[“carMake”]=>string(0)”“[“carModel”]=>string(0)”“[“submit”]=>string(6)”submit”}
@ShowmanPusha那么,数组键就存在了。从那时起,内容也应该被传输。试试马克的建议。@amartinno你还有其他建议吗?我已经尝试了建议的内容,但我没有luckIm猜测您指的是
,这会停止动态表的工作,当我单击submit时,我会得到与上面相同的输出。抱歉,我错过了脚本中对表的引用。答案已编辑,建议修改表和表单嵌套,而不是删除表。遗憾的是,如上所述,数组中似乎没有任何内容。在表单中使用$row变量似乎超出了while循环的范围,因此隐藏的输入为空。通过将其中一个隐藏字段设置为显式值(例如value=“xxx”),验证是否存在此问题。您可以通过从AddRowtTable函数中设置表单内的输入字段进行修复。我将regNo设置为“xxx”,输出
[“regNo”]=>字符串(3)“xxx”
,这表明您的诊断工作正常。所以在脚本中我应该放
$RegNo=$row[“RegNo”]$CarMake=$row[“CarMake”]$CarModel=$row[“CarModel”]
<?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" 
}
<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>