开发者

Problem with PHP number increment

I have a simple form that has a name and a message fields. When user submitted a form, the page display a User ID number what user has entered and a file "data.db" is created upon submission when it hasn't exist in server. The user ID number would begin from1 and increment by 1 every time a new submission is stored.

The file have the following format:

1|{name1}|{gender1}|{email1}|{phone1}
2|{name2}|{gender2}|{email2}|{phone2} ...

However, I am having trouble with display the user number, it just stayed at 1 every time the form is submitted.

The variable I created $userid doesn't seems to be incrementing at all...

Here is php code used for displaying the data after form submission...

 <?php
     include("email.php");

     $userid += 1;

  开发者_Python百科   echo "<b><u>Your submission has been added!</u></b> <br />";
     echo "User ID: " . $userid . "<br /><br />";
     echo "Name = ";
     echo $_POST["name"];
     echo "<br /> Gender = ";
     echo $_POST["gender"];
     echo "<br /> E-Mail = ";
     echo $_POST["email"];
     echo "<br /> Phone No. = ";
     echo $_POST["phone"];
     echo "<br />";
     echo "<p>Thank You</p>";

     $entry = $userid . "|" . $_POST["name"] . "|" . $_POST["gender"] . "|" . $_POST["email"] . "|" . $_POST["phone"] . "\n";

     file_put_contents("data.db", $entry, FILE_APPEND|LOCK_EX);

     echo "Confirm email sent!";
     ?>


The value of $userid is not kept on different loads of the page. You could store it in the $_SESSION global to access it, but that would only work if (a) you are using sessions, and (b) it is the same user at the same terminal in the same session.

What I think you want is that value to be used (and incremented) when any user views the page. You'd therefore need to store the value in, say, a file or an SQL database. Look it up on page load, increment it, write it back to disk. But then you have simultaneous access issues - what if two users open the page simultaneously - you may not get a unique [incremented] number.

I'd suggest using some kind of SQL back end, and having it auto-generate a unique ID to use in the output, thus guaranteeing you a unique userid each time.


As i wrote in the comment, your script has no way to know how often it is called. So you have to store the userid permanent.

you can use this (WARNING: i dont take care of filelhandling, eg. when 2 forms are submited in the exact same time):

<?php
 include("email.php");
 include('counter.php');

 $userid += 1;

 file_put_contents('counter.php', '<?php $userid = '.$userid.';');

 echo "<b><u>Your submission has been added!</u></b> <br />";
 echo "User ID: " . $userid . "<br /><br />";
 echo "Name = ";
 echo $_POST["name"];
 echo "<br /> Gender = ";
 echo $_POST["gender"];
 echo "<br /> E-Mail = ";
 echo $_POST["email"];
 echo "<br /> Phone No. = ";
 echo $_POST["phone"];
 echo "<br />";
 echo "<p>Thank You</p>";

 $entry = $userid . "|" . $_POST["name"] . "|" . $_POST["gender"] . "|" . $_POST["email"] . "|" . $_POST["phone"] . "\n";

 file_put_contents("data.db", $entry, FILE_APPEND|LOCK_EX);

 echo "Confirm email sent!";
 ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜