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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO UPDATE A DATABASE EVERY N MINUTES? 1

Status
Not open for further replies.

alexfusion

Programmer
Feb 5, 2001
94
0
0
AR
Hello there,

I'm trying my first steps in PHP,so this could be a simple question for the experts.

I have a simple HTML form which displays a Javascript clock,and other data in several fields.I have a little MySQL table where I need to insert the form data,but every 5 or 10 minutes.This HTML form is always visible,just because it's showing time and some fees for the user.
I'm working with Windows.
I have been looking for something similar to learn but I was not able to find anything.

Could anyone give me some help or some urls where to find info?

Thanks in advance.

alexfusion
 
Are you trying to submit and insert form data into the table every few minutes, or are you trying to update a page that displays the contents of the table every few minutes?

For an automatic form submittal, use javascript to submit the form every few minutes (using setTimeOut() and form.submit()). I'm not sure why you would want to do this though - I'd be interested to hear why.

For the table-contents page updating, use meta refresh (just simple HTML), or javascript (setTimeOut() and window.location).

Good luck! -gerrygerry
Go To
 
Hello gerry gerry,
Thanks a lot for your answer.
I'm trying to submit form data and insert into the table.
I was thinking about the Javascript way.
The reason I trying to do that is the following in short:

The form is displaying info for users in a litte cyber cafe.The proyect is simple.
The user log into the system,the form is showed with the clock running and the amount spent acording to the time.
I need to update all of this info every 4 or 6 or 10 minutes so in the case the client computer crash or hang up,(very possibly :), at least I get the most recent info stored in the table.

I tried with Javascript using setTimeOut() and submit().It works updating the table,but the window has to be all the time visible.I tried using $PHP_SELF as the action for the form so it has to remains visible,but I don't know if this is really a good solution.

If you have a better idea or suggestion I'd be very grateful.
Anyway I thank you very much for your response.

Kindest Regards

mixale
 
So, as I understand it, you are trying to maintain a database table containing a list of all the users currently on a specfic page? And you keep on submitting data from the user to the script to update the table's list of who's on the page?

Hmmmmmm... I think that a better way sould be to use sessions. Check this out: -gerrygerry
Go To
 
Hello gerrygerry,
I checked the url you gave me.It's good and similar to what I need but I didn't find a good explanation.
Let me explain it more clear.

It's a small cyber cafe.Suppose you are a user.You enter your username and password and a popup window is displayed showing the time you are spending and the amount charged.
This window is minimized after brief period,all of that as you know using Javascript.But of course you can
Once you log out,there will be a page showing:

Thank you Mr. gerrygerry for using our service.
Service usage : 1 hour 30 minutes.
Total : $ 1.25

That is the real project.Simple.But the popup window that shows time and amount has to send data to the table every N minutes.Why? If the computer you are using crash and the info (time and amount) was not stored into the table,you and me won't know how long you used the service and the amount to be charged.You'll be very upset if this happens :)
It's a basic and simple project.
I'm not trying to waste your time or everyone else's time just explaining this if I can find the solution for myself.Just I could not find a way to get the job done.

I thank you again for answering my questions.

Kindest Regards

alexfusion
 
So you need a constant display on the current charges (which is updated with DHTML), and every minute or so, you need to send this data to your php script to update the table, right?

I think the best way to do this would be to submit the form in another popup window that is never seen by the user. Then, your time/charge popup will not be interupted, the DHTML time/charge calculations will be submitted (every 1 minutes or so? it could be different if you wanted) for to your DB, and everything is fine, right?

You could just put something like this in your popup:
Code:
function submitData()
{
Code:
Code:
/* Construct the update URL from the DHTML updated form fields */
Code:
updateURL  =
Code:
"[URL unfurl="true"]http://your-domain.com/update_table.php?"[/URL]
Code:
;
updateURL +=
Code:
"time="
Code:
 +
Code:
document.form.time.value
Code:
;
updateURL +=
Code:
"&charge="
Code:
 +
Code:
document.form.charge.value
Code:
;
Code:
/* Position the popup at -1000, -1000 (out of the view of the user) */
Code:
window_params =
Code:
"left=-1000, top=-1000, screenx=-1000, screeny=-1000, resize=0, toolbar=0, status=0, scrollbars=0, location=0, menubar=0, directories=0, width=50, height=50"
Code:
;
Code:
/* Open the DB table update script */
Code:
window.open(updateURL, "update", window_params);
Code:
/* Do it all again in 60 seconds */
Code:
SetTimeout("submitData()",60000);
Code:
}
...
<body onLoad=&quot;SetTimeout('submitData()',60000);&quot;>

How's that sound? Does that makes sense? -gerrygerry
Go To
 
Hi gerrygerry,

I'm sorry it took me some time to write you back.
I was trying something similar to your solution,but I think yours is better.
That is exactly what I try to get done.Of course it makes a lot of sense!
Once again I must thank you for your help.It's very much appreciated!
Very good job! A new star for you, guy.

Best Regards

alexfusion

 
gerry's method seems to work fine, but the best route to do this would probably be to implement some sort of scheduled task on the client's machine. This way, you don't necessarily need to incur the overhead of creating a new browser window to track the user, and recreating it in the event the user closes said window. In addition, a user could possibly circumvent billing by disabling javascript (I'm not sure if you can predefine the browser security or not, so I might be totally wrong).

since it's a cybercafe, you have access to all the clients machines. Windows - use task scheduler, *nix - use cron. Have the user login via some gui, and set it up to run every x minutes. Unfortunately, clientside scheduled tasks are out of the scope of this forum. (Windows can run vbs and I *think* javascript, *nix uses perl,python,bash,etc. and I *think* php if you configure php correctly).

If you're interested in this method, post back and I'll give you some more info about it.

good luck - leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
Hello there,

First of all I have to thank gerrygerry for the thankful words.
About the possibility of implementing task scheduler ( I'm working with Windows) proposed by you Leo,it's very interesting.I'd be very grateful if you can give some help about how client side scheduled tasks could be implemented.
Since I see the chance that the client could disable javascript and you are right on that.
Thank you to you both for the great help!

Kindest Regards

alexfusion

 
I thought of another thing - you could also put in a frame that has no size (a thin row at the top of your page that has no height?). In this frame, you could access the contents of the bottom frame, and update it's location with javscript. You could use an
Code:
onLoad=&quot;this.location = 'process_charges.php?time='+parent.bottom_frame.your_timer+'&charges='+parent.bottom_frame.your_charges+'';&quot;

If you did this, you'd wanna put some code in your bottom frame that ensured the cybercafe was loaded into the frameset.

Checking to make sure the user has javascript enabled would be an easy thing to do. If they dnn't have it enabled, dont load the cybercafe. I'm not excatly sure how much control you have over your cybercafe, or even what a cyber cafe is, but if you are already relying on DHTML to update the user's charges, worrying about javascript being enabled isn't much of a concern, is it?

Did I just complicate things for you? I hope not! I think that using the frames might be better though. It'd be easier for browser compatability.

Plus, a &quot;pop-up stopper&quot; program could make you very poor, quickly! I'm sorry - I should have thought of that!

One more idea, what if you used sessions and a php timer class? If you did all your charge processing server-side, what is there to worry about? You could just use meta-refresh in the top frame, and the sessions would keep your timer running, no? You could still use DHTML to give rough approximations of time/charges to the user, but the real thinking would be done on the server and be out of the control of a user, right? Although, people can also disable the meta-refresh (in IE6 anway), so using some javascript checking would still be in order.

I think I wrote too much and I'm afraid I may have just discouraged you, but using sessions would be a good way to do it. Maybe you could even simplify it by using a timer class. Try .

Check out these URL's - they might help you out too (similar things):

I hope this helped! -gerrygerry
Go To
 
Hello gerrygerry,
Thank you again for answering.
I'll check the url's you gave me.That's a lot of information,but I like it.I will let you know how I go.

Thank you so much.

alexfusion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top