in my project i'm using user input form. that form contains serial no, reference no, name, email, phone, address, etc.., in that user input form serial number textfield is readonly option. user can't able edit the serial number. in that textfield i want to show the next auto_increment id from mysql database. how to do that? for example if i have entered 150 rows in mysql database. then whenever user enter that input form serial number textfield should show 151 (next auto_inc id). any help ???

<tr>
    <td width="175" height="30" class="smalltext">Serial Number :</td>
    <td width="214" class="textbox"><input style="font-weight:bold;" type="text" name="serial" value="<?php       ?>" readonly="readonly"/></td>
    <td width="511">&nbsp;</td>
  </tr>
  <tr>
    <td width="175" height="30" class="smalltext">Reference Number :</td>
    <td width="214" class="textbox"><input style="font-weight:bold;" type="text" name="reference" value=" 
    <?php 
    function gen_random_string($length=6)
    {
        $chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        $final_rand ='';
        for($i=0;$i<$length; $i++)
        {
            $final_rand .= $chars[ rand(0,strlen($chars)-1)];
        }
        return $final_rand;
    }
    echo gen_random_string()."\n"; //generates a string 
    ?>" readonly="readonly" /></td>
   <td>&nbsp;</td>
  </tr>

in that textfield i want to show the next auto_increment id from mysql database

That's not a good idea. Suppose 5 people open your page at the same time. They will all show the same next ID, but after they submit a different ID will actually be used. Users shouldn't have any knowledge about internal ID's.

oh ok ok. but this is not a online project. this is the billing software. like invoice billing software. so, only one person can edit the billing details.

i got it pritaeas.. but your comments also valuable comment. this is the coding for get the next auto_increment id from mysql

<?php
// database connection
// select your database
$result = mysql_query("SHOW TABLE STATUS LIKE 'your_table_name'");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
echo $next_increment;
?>

only one person can edit the billing details.

That maybe true, but he can still have two screens open. Never trust a user.

Anyway, you can retrieve the MAX(id) and add 1, but that doesn't work if you allow deletions on that table. The safest method is to retrieve the value from information_schema.tables for your database and table.

Member Avatar for diafol

As pritaeas states, don't ascribe a serial no before a record is inserted - there's absolutely no need to do this. You can show the serial number on completion of INSERT of course with the mysql_insert_id() or mysqli_ equivalent function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.