Solved

How to make a running number(ex:001,002,003,etc) appear in a textbox

Posted on 2008-10-30
7
348 Views
Last Modified: 2013-12-13
Dear Experts,
i wanna ask, how to make a running number (ex:001,002,003,...etc) appear in a textbox. note,  i dont want to make this running number from auto increment in mysql database, but i wanna describe it manually.also this running number automatically increment after i push a submit button(this submit button are a Saved Query into database).in other word, after i click my submit button,this running number automatically increased.i am using php 5,javascript and mysql database.

many thanks...
Regards
Louis
0
Comment
Question by:antonius_louis
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:ionutz2k2
ID: 22838771
I think what you need is a $_SESSION variable (ex. $_SESSION['myIndex']) that stores that number you want to increment. The script that is run when you press the submit button increments that variable and the script that creates the page and shows that number will show the incremented value.
0
 
LVL 17

Expert Comment

by:psimation
ID: 22839089
This will submit the form to itself and have an incremented number already in textbox when you submit.

Add your SQL insert code below the if statement ( check for $_POST[submit] etc before you do your sql)

<?php
session_start();
if (isset($_SESSION[count]) {
$_SESSION[count]++;
} else {
$_SESSION[count] = "1";
}
?>
<form name="form" method="post" action="">
<input type="text" name="textbox" value="<?=$_SESSION[count]"?>">
<input type="submit" name="submit>
</form>

This code will work "per user" and for the length of the session only.

If you want a number that increments for ALL users who use your site, then it's going to become really tricky.

1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.

HOWEVER, this will possibly cause duplicate numbers on busy sites, since there is a timelag between reading, incrementing and updating, meaning two users who hit your site can potentially get the same number back from the table...

What is the purpose of this number you want to have displayed ( also, remeber that displaying the number inside a texfield means a user can change it...)

If you want to indicate or use it as part of a membership number, then I suggest you only generate that number AFTER the data has been entered into the table, then get the "last insert id" back and use that number as part of your unique sequenced number...



0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22844432
If you want to control this number for the duration of a session, put the number in the session.

If you want to control this number on a per-user basis and have it persist beyond the session, put the number into the user records in your data base.

If you want to control this number across the entire population of uses in the data base, use LOCK TABLES while retrieving and updating the number.

Tell us a little more about what you want to use the number for, and we can give you a more succinct explanation.

Best regards, ~Ray
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:antonius_louis
ID: 22848538
for All  Experts:Thanks for the explanation...

for Psimation : could you give me a specific expalanation about your comment ("1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.").

regards
0
 
LVL 17

Expert Comment

by:psimation
ID: 22848667
OK, very basic example...

//create a new table called number_table containing only one collumn called "number" of type int size 255
//connection strings here
$sql = "select number from number_table";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$number = $row[number]; // <<== now you have the last number that was writen to table
//now use the number
echo '<input type="text" name="whatever" value="'.$number.'">';
//now increment number and write back to table
$number++;
$sql_update = "update number_table set number = \"$number\";
$result_update = mysql_query($sql_update);
//fin

Now, just remeber the comment from Ray_Paseur re locking tables.

I'm sure he can give you an example of doing that ( you need to add extra SQL to do that)


0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22854097
Man Page here: http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

The query sequence would be like this:

LOCK TABLE number_table WRITE
SELECT number FROM number_table
/* USE NUMBER as $number */
$number++;
UPDATE number_table SET number = $number
UNLOCK TABLES

Now in the spirit of 100% truth, you do not absolutely have to use MySQL for this - you could also use a flat file, but the ease with which you can lock and unlock tables makes MySQL the easier-to-code answer.

And another note: Minimize the time and risk of script termination between SELECT and UPDATE- you do not want to keep a table locked longer than the minimum!

Best regards, ~Ray
0
 

Author Closing Comment

by:antonius_louis
ID: 31511564
works great. thanks a lot...

regards
Louis
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now