简体   繁体   中英

How can I use autoincrement without the help of database in php

I have a form which user submits and is sent directly to the e-mail. The problem is that I'm not storing these values in database and directly mailing them. It is like a complaint registering system. What I want to do is that when the user submits a complaint and is redirected to success page, a complaint number is generated which should be obviously incremental by 1 for the next submission. Also there is no separate user account as anyone visiting the website can submit complaints. I tried using a field option as unique id but it didn't really work. The html form is,

    <form method="post" action="handler.php">

    <div>
        <label for="first_name"><span class="labelname"><strong>First Name:</strong></span></label> 
        <input type="text" maxlength="50" size="50" name="first_name" id="first_name" value="" class="required" />
    </div>

    <div>
        <label for="last_name"><span class="labelname"><strong>Last Name:</strong></span></label> 
        <input type="text" maxlength="50" size="50" name="last_name" id="last_name" value="" class="required" />
    </div>

    <div>
        <label for="telephone"><span class="labelname"><strong>Telephone Number:</strong></span></label> 
        <input type="text" maxlength="20" size="50" name="telephone" id="telephone" value="" class="required" />
    </div>


    <div>
        <label for="email"><span class="labelname"><strong>E-mail: (Optional)</strong></span></label> 
        <input type="email" maxlength="30" size="50" name="email" id="email" value="" class="" />
    </div>



    <div>
        <label for="com_type"><span class="labelname"><strong>Complaint Type:</strong></span></label>   
        <select name="com_type" id="com_type" class="required">
         <option value=""></option>
         <option value="Electrician">Electrician</option>
         <option value="Plumber">Plumber</option>
         <option value="Mason">Mason</option>
         <option value="Miscellaneous">Miscellaneous</option>
        </select>
    </div>

    <div>
        <label for="flat_no"><span class="labelname"><strong>Flat No.:</strong></span></label> 
        <input type="text" maxlength="10" size="50" name="flat_no" id="flat_no" value="" class="required" />
    </div>

    <div>
        <label for="block_no"><span class="labelname"><strong>Block Number:</strong></span></label>   
        <select name="block_no" id="block_no" class="required">
         <option value=""> </option>
         <option value="A-1">A-1</option>
         <option value="A-2">A-2</option>
         <option value="A-3">A-3</option>
         <option value="A-4">A-4</option>
         <option value="A-5">A-5</option>
         <option value="A-6">A-6</option>
         <option value="A-7">A-7</option>
         <option value="B-1">B-1</option>
         <option value="B-2">B-2</option>
         <option value="B-3">B-3</option>
         <option value="B-4">B-4</option>
         <option value="C-1">C-1</option>
         <option value="C-2">C-2</option>
        </select>
    </div>

    <div>
        <label for="message"><span class="labelname"><strong>Describe your problem:</strong></span></label>
        <textarea rows="10" cols="50" maxlength="2000" name="message" id="message" class="required"></textarea>
    </div>

       <button class="submit" type="submit" name="submit" value="Send Email">Submit Complaint</button> <button class="reset" type="reset">Reset</button>

php code,

<?php
if(!isset($_POST['submit']))
{
die("Error. You need to submit the form.");
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
$visitor_email = $_POST['email'];
$com_type = $_POST['com_type'];
$flat_no = $_POST['flat_no'];
$block_no = $_POST['block_no'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Complaint";
$email_body = "message\n\n". "First Name: $first_name\n\n". 
"Last Name:  $last_name\n\n".
"Telephone: $telephone\n\n". "Complaint Type: $com_type\n\n". 
"Flat No.: $flat_no\n\n". "Block No.: $block_no\n\n". 
"Complaint: $message";

$to = "my email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
mail($to,$email_subject,$email_body,$headers);
//success, redirect to thank

header('Location: http://mywebsite.com/thank.php'); 
} catch(Exception $e){
//problem, redirect to fail
header('Location: http://mywebsite.com/fail.php'); 
}       
?>

I just want a complaint number on the successful submission page and the complaint number should also go in the mail also with other details. Can I do it without using the database. Please help.

If you have the numbers stored as a primary key, then you can do something like this:

SELECT COUNT(*) FROM `table`;

Or if you have the auto_increment or PRIMARY KEY set, you can use:

SELECT MAX(`id`) FROM `table`;

And then add + 1 to the result and insert it as new. If you aren't using a database, then use a flat file named count.txt for it and put in the current number and increment:

<?php
  $count = file_get_contents("count.txt");
  $count++;
  file_put_contents("count.txt", $count);
?>

But this option is not so good. So please use a mechanism to lock the file while updating the count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM