user1515503
user1515503

Reputation: 71

Printing next row ID of MySQL row after page is reloaded

I have a simple form where I am dynamically inserting the values from one MySQL table:

<input type="text" name="title" value="1" size="45">

This value="1" is coming from one MySQL table whose id parameter is auto-incrementing. So after id=1 it is 2,3,4,5,6,7,8,9 etc

When I submit the form, it is again reloaded but I want the id to be 2 next time. Then the third time, it should be 3, then 4, then 5, etc...

$res16 = sql_query("select * from mlf2_entries order by id desc limit 1 ");
    $row = sql_fetch_array($res16);
$textqb = $row['id'];   

^ I tried this but it always displays value="1"

Upvotes: 1

Views: 457

Answers (1)

Zane Bien
Zane Bien

Reputation: 23125

What you can do is use the offset feature in MySQL's LIMIT clause to get the next row. This also accounts for any sequential gaps there may be in the id field:

SELECT * FROM mlf2_entries ORDER BY id LIMIT 0,1

^ This gets the first row in ascending order of the id field.

The first number in LIMIT clause is the offset. The second number being the number of rows returned (you only want one row, so you can leave that number the same).

To offset to the second row, do LIMIT 1,1... to offset to the third row: LIMIT 2,1, and so forth and so on.

Upvotes: 5

Related Questions