我有一个表单,它向数据库发送信息(填写时)
表格功能:
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)它获取并且id为1并完成。
然后页面重定向到show_axle_table.php。函数应该获取最后插入的id。但它显示我的ID为2。
此外,我希望ID显示在我的页面顶部,如:
show_axle_table.php?train_id="<?php ['$id'] ?>
但是现在它什么都没有显示出来。 (train_id =)
答案 0 :(得分:1)
像这样更新:
<?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();
}