我有一个表格,它将信息发送到数据库(填写时)
表单功能:
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');
}
对。因此,当填写表单时,它会被发送到数据库(works)。
用户将在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=)
更新如下:
<?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();
}