user3283373
user3283373

Reputation: 69

How to assign auto generated form number to new form which has to be filled next

I have website in which i feed daily entry of complaint forms , Now we don't assign form number to any from, in database from number is a id of all from and it auto generated by database but i want show this id on a html from before from submitted ,

like if i click new from on my web site it will show all filed empty except form no filed it'll be filled by auto generated from no , and when its submitted it'll take this from no as id if it'll be available that time ,

Upvotes: 0

Views: 1981

Answers (2)

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

I suggest to use UDF (User Defined Function) to achieve this.

Submitting a pre-filled value into an auto-incremented value may result an exception if multiple forms, by different user, with same number are submitted. There would be a constraint violation.

Instead you can depend on UDF for sequences on required table field.
You may require:

  1. Define a table to store sequence numbers, that are auto incremented.
  2. Define a function nextval to generate next sequence number record into the table.
  3. Define a function curval to fetch current sequence number record from the table.
  4. Use the nextval function to fill your form for "Form Number" field.
  5. On submitting the form, insert the same value into your form table in database.

This procedure is safe on concurrent submissions.

Refer to similar discussion and an example at:

Upvotes: 1

Mani
Mani

Reputation: 888

try this one

$ids =mysqli_num_rows(mysqli_query($con,"SELECT MAX(id) from mytable"));//con is connection variable
 if(!empty($ids[0])) 
     $form_no = $ids[0]+1;
 else
     $form_no = 1;

Upvotes: 0

Related Questions