Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run PHP script at page load

Status
Not open for further replies.

GWHicks

Programmer
Dec 11, 2002
39
US
I am trying to retrieve a record from my database when a webpage loads. Basically I need it to check the database, find the highest record ID, increment it by 1, and then post that number on the webpage to tell my user what record number they are about to create.
I think I have the script right for pulling the number (except for incrementing it by 1, right now it will just pull the highest numbered record ID and display it, incrememnting is something I can figure out I am sure).
What I am trying to figure out is how to get it to execute the script at page load. I have a different script that runs when the user clicks the submit button to save the current record, but how do you make a database query run at the load of an html page?
Thanks guys!
Greg

Greg Hicks
VB.Net (Newbie) Programmer
 
You can't. PHP runs on the server, "on load" code on the client. You can't just call PHP code at any arbitrary location on a page. Remember the web paradigm: the client optionally sends data and fetches a page, the server responds, the connection ends. Unlike an application that's running on a client machine, there is no continuous interaction between the server and the client.


Also, your idea of fetching the next id and putting it on the page is a bad one. You have to keep in mind that web programming is multi-user programming.

Suppose that userA runs the initial script and gets the max id of the page plus one. He gets 3. Then suppose that userB then runs the same script. He'll also get a 3 if he runs the first script before userA has submitted data to be inserted into your database. If you're using that value for something important, like future record ids for storing data, then you will have a data collision.

I recommend that you insert a blank record into the database, and let your database server assign the new value. Then fetch that value and include it not only in the user-viewable page, but also in a hidden form field on the page.

When the page is submitted, your script can then use the passed record id to update the appropriate record in the database.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir:
the onlyproblem i found with auto increment is that it "saves" the highest id in "memory". suppose there is a database:
1
2
3
4
5


the next number will be 6.
if i delete 5 from the database ie:
1
2
3
4

even then the next number is 6. i tried the mysql documentation but all i could understand is that u have to delete all the records. then only is the memory reset...


Known is handfull, Unknown is worldfull
 
vbkris:
Thank you for posting that random piece of MySQL trivia. I don't know why this behavior seems to disturb you -- the behavior you describe has always sounded perfectly reasonable to me.

However, no one here has mentioned deleting records from a MySQL table with an auto_increment column. GWHicks had mentioned insertion, I've mentioned insertion and update.

In fact, GWHicks hasn't specified what database server he's using.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ah, seems like almost anything can be turned into a debate these days. (-: vbkris is right in his assumption of my use of MySQL, and that most likely stems from all the hoopla surrounding the phrase "If you are using PHP you HAVE to learn MySQL, they work so well together!" While that statement is both true AND false it is also neither hear nor there in this discussion.

But I digress.

I understand about the issues surrounding pulling a number from the database ahead of creating a new record. That is not of a major concern to me, however your idea is probably the best route to go. Create a blank record (save for the ID) then return that ID to my form that the user is filling out. I could end up with garbage in my database if users start to create a new record (INSERT a new ID) but never submit their form but I would think this would not really make the database any larger then normal, just storing some useless ID numbers, but again, that is another discussion.

This still leaves me in the position of how to invoke a new record in the database when the user pulls up a form. Considering this now I am not sure this is even the best way to do it, otherwise the issue I mentioned above would be compounded by people viewing the page and then not following through. Being that hindsight is ALWAYS 20/20 I would deduce that a better way to handle this is to not even put the ID number anywhere on my form. My user doesn't need to see that number until AFTER they have submitted their data. So I will generate a response page to the user, showing the information they have submitted, and on that page will be returned the ID number assigned to their record. This prevents all of the previous issues as discussed herein! Thanks a bunch guys!

Greg Hicks
P.S. vbkris, there is a way to "reset" the auto assigned number back to a base number. I had to figure this out when I was doing testing in a .Net app I was writing. I had started with some bogus numbers in my autoincrement field and when done wanted it to start counting from the current number we were using in our old system. However, it kept displaying the "feature" you mentioned, and even deleting ALL records from the table didn't fix it. There is a good reference on it here:
Resetting auto-increment value in MySQL
Thanks again guys.

Greg Hicks
VB.Net (Newbie) Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top