hi hope someone can help me out how do i include a autoreferece or auto code inside my php form so each form that i submitted will have its own number or an id of some sort to track how many form have been submitted.when form is sent to email the receiver will see which the number reference or ID.

thanks hope someone can help

Dani AI

Generated

Quick summary tied to the thread: , your flat-file idea will work but needs locking and careful error handling; is correct that a database AUTO_INCREMENT is the simplest and most reliable choice for concurrent submissions. asked for details — below are two short, practical patterns (DB first, then file) and a few cautions so the ID you email is safe and consistent.

Database (recommended): insert the form data and read the inserted id from the same DB connection. Using PDO:

// assume $pdo is a connected PDO instance
$stmt = $pdo->prepare("INSERT INTO submissions (name,email,message) VALUES (:name,:email,:message)");
$stmt->execute([':name'=>$name, ':email'=>$email, ':message'=>$message]);
$id = $pdo->lastInsertId();               // reliable immediately after insert
$reference = sprintf("REF-%s-%05d", date("Ymd"), $id);
// include $reference in the email subject/body and also save it in the row if you want a human-friendly code

Flat-file counter (if you cannot use a DB): use an exclusive lock so two requests cannot increment at once.

$counterFile = __DIR__ . '/counter.txt';
$fp = fopen($counterFile, 'c+');
flock($fp, LOCK_EX);
rewind($fp);
$contents = stream_get_contents($fp);
$counter = ($contents === false || trim($contents) === '') ? 0 : (int) trim($contents);
$counter++;
rewind($fp);
ftruncate($fp, 0);
fwrite($fp, (string)$counter);
fflush($fp);
flock($fp, LOCK_UN);
fclose($fp);
$reference = sprintf("REF-%s-%05d", date("Ymd"), $counter);

Cautions and tips: always fetch the insert id on the same DB connection and immediately after the insert; use transactions if multiple tables must be consistent; for high traffic or multi-server setups, a DB (or centralized counter service) is the only safe option; add a readable prefix or date to avoid exposing raw internal IDs; validate and log failures so you can reconcile emailed references with stored records.

Recommended Answers

All 5 Replies

explain more with what you have so far!

hi iv been googleing and got a few ideas one suggested using a flat file and add a digit (id) to start with and auto increment from that number from each form submited and each time a new form is submited write the new auto increment id to the flat file and so on.

Im looking for a simple and fast way to do that I have my form data and also a variable that will submit the id along with my form data.

Member Avatar for Member #120589

DB is safer and more reliable. Autoincrement is easy with that.

Yes I know database is a lot safer but the down side to it i have to create the database create function to generate the number write it to database and again fetch it to email with my form data?

If you have a better way or simpler using DB please let me know.thanks

Member Avatar for Member #120589

id (autoincrement) | submit_id (user id)

? is that it?

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.