-1

I want to keep my last inserted record in the same input field after pressing submit button.

How can retrive last record from database?

I also could not keep the record in the same input field after refresh the page.

<?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']; ?>">

insertion coding

<?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');
    }
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
kailashkumar
  • 43
  • 2
  • 11

2 Answers2

0

this might help you.

$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)));
}

In view page:

    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'] : '' ?>">
Sinto
  • 3,915
  • 11
  • 36
  • 70
0
mysqli_query($conn, $sql)) 
$last_id = mysqli_insert_id($conn);
  • Please describe where this code has to be put and what it does. – Al0x Nov 24 '17 at 12:04
  • To improve the quality of this Answer, please include an explanation of how your suggestion works, as well as a larger sample of code that will more clearly identify where your code should be placed in relation to their code - perhaps even include a copy of their code modified to use your suggestion. – toonice Nov 24 '17 at 12:44
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Isma Nov 24 '17 at 13:19