lastInsertId 从不同的函数获取

lastInsertId get from different function

我有一个表格,将信息发送到数据库(填写后)

表格函数:

function train_add() {
            $sql = "INSERT INTO train_information "
        . "(train_id, image, train_name, tare_weight, number_of_bogies, number_of_axles, wheel_diameter_min, wheel_diameter_max)"
        . "VALUES (train_id, :image, :train_name, :tare_weight, :number_of_bogies, :number_of_axles, :wheel_diameter_min, :wheel_diameter_max) ";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':image', $_POST['image'], PDO::PARAM_STR);
            $sth->bindParam(':train_name', $_POST['train_name'], PDO::PARAM_STR);
            $sth->bindParam(':tare_weight', $_POST['tare_weight'], PDO::PARAM_STR);
            $sth->bindParam(':number_of_bogies', $_POST['number_of_bogies'], PDO::PARAM_STR);
            $sth->bindParam(':number_of_axles', $_POST['number_of_axles'], PDO::PARAM_STR);
            $sth->bindParam(':wheel_diameter_min', $_POST['wheel_diameter_min'], PDO::PARAM_STR);
            $sth->bindParam(':wheel_diameter_max', $_POST['wheel_diameter_max'], PDO::PARAM_STR);
            $sth->execute();
            return $this->pdo->lastInsertId('train_id');
    }

没错。因此,当填写表格时,它会发送到数据库(有效)。

用户将在 10 秒后被重定向(我自己做了这个以查看是否出现任何错误)。

<?php
        //Train information is send to the database, and selects the ID of the just added train//
        $add_train = $database->train_add();
    ?>

    <meta http-equiv="Refresh" content="10;url=http://localhost:8080/show_axle_table.php?train_id="<?php ['$id'] ?> >

show_axle_table.php:

<?php
    $show_axle = $database->axles();
?>
    <div id="train_select_table">
        <table>
            <tr>
                <th>Train id</th>
                <th>Number of bogies</th>
                <th>Number of axles</th>
            </tr>
        <div id="loopRow">
            <?php      
                foreach($show_axle as $res){
                //Loop trough results, generate a tablerow every time
            ?>
            <tr>        
                <?php
                echo "<td>" . $res['train_id'] . "</td>";
                echo "<td>" . $res['number_of_bogies'] . "</td>";
                echo "<td>" . $res['number_of_axles'] . "</td>";
                ?>
            </tr>
        <?php
            }
        ?>
        </div>
        </table>
        </div>

和函数:

function axles(){
        $id2 = $this->train_add($id);
        $sql = "SELECT * FROM train_information WHERE train_id = '$id2'";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":id2", $_GET["train_id"], PDO::PARAM_STR);
        $sth->execute();
        return $sth->fetchAll();
    }

现在 table 显示了火车的 ID。但它加 1,所以其他一切都是空的。 例如: 用户填写表格。将它发送到数据库 (train_add) 它得到 id 为 1 并完成。

然后页面重定向到show_axle_table.php。函数应该获取最后插入的 id。但它显示 ID 为 2。

我还希望 ID 显示在我的页面顶部,例如:

show_axle_table.php?train_id="<?php ['$id'] ?>

但现在它什么也没显示。 (train_id=)

像这样更新:

<?php
        //Train information is send to the database, and selects the ID of the just added train//
        if (!(isset($_GET['train_id]) && $_GET['train_id'])) {
            $add_train = $database->train_add();
        }
    ?>

    <meta http-equiv="Refresh" content="10;url=http://localhost:8080/show_axle_table.php?train_id="<?php echo $add_train; ?> >



<?php
    $show_axle = $database->axles($_GET['train_id']);
?>
    <div id="train_select_table">
        <table>
            <tr>
                <th>Train id</th>
                <th>Number of bogies</th>
                <th>Number of axles</th>
            </tr>
        <div id="loopRow">
            <?php      
                foreach($show_axle as $res){
                //Loop trough results, generate a tablerow every time
            ?>
            <tr>        
                <?php
                echo "<td>" . $res['train_id'] . "</td>";
                echo "<td>" . $res['number_of_bogies'] . "</td>";
                echo "<td>" . $res['number_of_axles'] . "</td>";
                ?>
            </tr>
        <?php
            }
        ?>
        </div>
        </table>
        </div>

并作为

发挥作用
function axles($id){
        $sql = "SELECT * FROM train_information WHERE train_id = '$id'";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":id2", $_GET["train_id"], PDO::PARAM_STR);
        $sth->execute();
        return $sth->fetchAll();
    }