1

I have an id that i am trying to read from my database and if it is equal to or less then the id i want to id++ until the id is not in the database and use that for the id of the form. Problem is i don't know how to make a while loop that starts at number one and checks the database for that id if it exists then step the id until the id is not in the database and use that id for the form that is entered this is so a user can fill out a form and it emails a pdf to the user and another person the other person can pull up the form and edit it aka enter a new piece of data or the user can correct or change something on the form without reentering all the data. i have all the forms ready. i have a page to enter the id and pull up the form and it is already storing the data but i cant figure out how to make it generate a generic id for the order.

2 Answers 2

1

You should probably not manually be setting the ID in your table. Set your id field to the PRIMARY KEY make it an INT and UNSIGNED, last turn on AUTO INCREMENT. From there, the DB will automatically find the next value for your id and use it.

Then in your php code, insert the newly filled out form into the database. and get the last insert id, to make a link to this prefilled form:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());

Example off of php.net

Additional Code block as requested in comments:

mysql_query("INSERT INTO mytable (product) values ('kossu')");

// GETTING THE ID
$id = mysql_insert_id(); 

//USING THE ID
$body = "THIS IS AN EMAIL MESSAGE BODY, GO TO http://example.com/form.php?id=" . $id; 
mail('[email protected]', 'My Subject', $body);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to bother you again but i want to display this id in a email. How would i put the id from this after some text in a email normally i would use the post data "$_POST[id]" to do this but since it is mysql data i don't know how to display this in a line of text
1

It is not necessary to find free IDs yourself. You can use an AUTO_INCREMENT column and let the database pick an ID for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.