【发布时间】:2015-06-13 01:31:19
【问题描述】:
我有一个表格,将信息发送到数据库(填写后)
表格功能:
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();
}
现在表格显示了火车的 ID。但它加了 1,所以其他一切都是空的。 例如: 用户填写表格。将它发送到数据库(train_add)它得到 1 的 id 并完成。
然后页面重定向到 show_axle_table.php。函数应该获取最后插入的 id。但它显示我的 ID 为 2。
我还希望 ID 显示在我的页面顶部,例如:
show_axle_table.php?train_id="<?php ['$id'] ?>
但现在它什么也没显示。 (train_id=)
【问题讨论】:
标签: php oop pdo mysqli phpmyadmin