简体   繁体   中英

How to send latest records from database to html texbox after submitting a form?

Steps :
1) select record from database

2) assign record to html textbox

3) user change value in textbox

4) user click "Save" html button.

5) form submit method="post" action="current-page"

6) get value from textbox

7) update record in database table

Explanation : page is refreshed after form submit, so selecting record from db is happened before updating database, that's why html textbox cannot get latest records. How can i assign latest records to textbox after form is submited?

When step 5 is executed, page will refresh. So step 1 and 2 will be executed before step 7, that's why textbox on step 3 cannot get latest record. How to make step 7 happen before step 1?

Problem is solved. I just move up the code of step 7 to on top of step 1.

put an if condition on the same page if form is submitted, update query or when page loads, you can query the latest records

$flag=0;
$submit=$_POST['submit'];
if($submit=="Yes") {
//update database with new value
}
else {
$flag=1;
//query latest value from DB
$latest=$row['latest'];
}

in html form

<input type="text" value="<?php echo $latest; ?>" />

if you don't want the form to be displayed after submit, use the flag

if($flag) { 
//print form
}
else {
// echo data entered
}

You should store temporarily the latest row's unique identifier in your session. That's the easiest way, if your table is not sorted on a way you could achieve it by sorting - like adding an "inserted_time" column with NOW() values and sort by that, get the latest.

I'm not completely sure I understand, but I think mysql_insert_id() might help.

http://www.php.net/manual/en/function.mysql-insert-id.php

After you save the data to the db, grab the last id and use it in your new query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM