簡體   English   中英

我如何在沒有數據庫幫助的情況下使用自動增量

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

我有一個用戶提交並直接發送到電子郵件的表格。 問題是我沒有將這些值存儲在數據庫中,而是直接通過郵件發送它們。 就像投訴登記系統一樣。 我想做的是,當用戶提交投訴並將其重定向到成功頁面時,將生成一個投訴編號,對於下一次提交,投訴編號應該明顯增加1。 此外,沒有單獨的用戶帳戶,因為訪問該網站的任何人都可以提交投訴。 我嘗試使用字段選項作為唯一ID,但實際上沒有用。 html形式是

    <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代碼,

<?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'); 
}       
?>

我只想在成功提交頁面上找到一個投訴編號,該投訴編號也應連同其他詳細信息一起發送到郵件中。 我可以不用數據庫就可以做到嗎? 請幫忙。

如果您將數字存儲為主鍵,則可以執行以下操作:

SELECT COUNT(*) FROM `table`;

或者,如果您設置了auto_incrementPRIMARY KEY ,則可以使用:

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

然后將+ 1添加到結果中並將其作為新插入。 如果您不使用數據庫,請count.txt使用一個名為count.txt的平面文件,並輸入當前數字並遞增:

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

但是這個選項不是很好。 因此,請在更新計數時使用一種機制來鎖定文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM