使用php将最后插入的记录保存在输入字段中

时间:2016-01-27 08:42:08

标签: php

我想在按下提交按钮后将我最后插入的记录保存在同一输入字段中。

如何从数据库中检索最后一条记录?

刷新页面后,我也无法将记录保存在同一输入字段中。

<?php
include 'db.php';
$qq=mysqli_query($connect,"select * from tbl_route");
?>
Route:
<select name="route" id="route">
    <option value="" disabled selected> : : Select : : </option>
    <?php
    $n=1;
    while($a=mysqli_fetch_array($qq))
    {
        ?>
        <option  value="<?php echo $a['r_id']; ?>"><?php echo   $a['route1']; ?></option>
        <?php
        $n++;
    }
    ?>
</select>
Driver:
<input type="text" name="driver" id="driver" value="<?php isset($_POST['driver']) echo $_POST['driver']; ?>">

插入编码

<?php
if(isset($_POST['submit']))
{
    session_start();   
    include 'db.php';

    $route = $_POST['route'];
    $driver = $_POST['driver'];
    $driver_name = $_POST['driver_name'];
    $vehicle = $_POST['vehicle'];
    $passenger = $_POST['passenger'];
    $date=date('Y-m-d H:i:s');
    $status=1;

    $query = mysqli_query($connect,"INSERT INTO `motorpark-db`.`tbl_trip`  (`route`, `dm`, `dn`, `vehicle`, `passenger`, `t_status`,  `trip_date`)VALUES('$route', '$driver', '$driver_name', '$vehicle', '$passenger', '$status', '$date')");
    $q=mysqli_query($connect,"select * from tbl_trip where route='$route' and dm='$driver'");

    $row=mysqli_fetch_array($q);
    $route=$row['route'];
    $dm=$row['dm'];
    $dn=$row['dn'];

    $_SESSION['route']=$route;
    $_SESSION['dm']=$dm;
    $_SESSION['dn']=$dn;

    if($query)
    {
        header('location:trip_details.php');    
    }
    else
    {
        header('location:trip_details.php');
    }
}
?>

2 个答案:

答案 0 :(得分:0)

这可能对你有帮助。

$query = mysqli_query($connect,"INSERT INTO `motorpark-db`.`tbl_trip`  (`route`, `dm`, `dn`, `vehicle`, `passenger`, `t_status`,  `trip_date`)VALUES('$route', '$driver', '$driver_name', '$vehicle', '$passenger', '$status', '$date')");
$last_inserted-id = mysqli_insert_id($connect);
$get_record = mysqli_query($connect,"select * from tbl_trip where id = '$last_inserted-id'");
$row=mysqli_fetch_array($get_record);
if($query)
{
    header("location:trip_details.php?vals=" . urlencode(serialize($row)));
}

在视图页面中:

    if (isset($_GET['vals'])) {
        $Values = unserialize(urldecode($_GET['vals']));
    }
    <select name="route" id="route">
    <?php
    $n=1;
    while($a = mysqli_fetch_array($qq))
    {
        ?>
        <option  value="<?php echo $a['r_id']; ?>" <?php if (isset($Values['route'])) { if ($Values['route'] == $a['r_id']) { ?> selected <?php } }?>><?php echo $a['route1']; ?></option>
        <?php
        $n++;
    }
    ?>
</select>
    <input type="text" name="driver" id="driver" value="<?php (isset($Values['driver']))? $Values['driver'] : '' ?>">

答案 1 :(得分:0)

mysqli_query($conn, $sql)) 
$last_id = mysqli_insert_id($conn);