Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

A mysql query("UPDATE") Problem

4:16 pm on Jun 20, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 9, 2009
posts: 43
votes: 0

I'm learning PHP, JS and mySQL and I'm having trouble putting it all together.

I've created a JS countdown timer which is set using:

The three variables (h,m,s) contain values I'm retrieving from my database, which is working correctly.

However, I can't seem to piece together a way to send the new values back to the server to be retrieved again on the next loop.

Here is my current (watered down - sorry if it's still a bit lengthy) code:

var serverTime = '<?php print date("F d, Y H:i:s", time())?>'
var timeLeft = new Date(serverTime)
var t;
var timeLeft1 = timeLeft
var h = //I'm pulling this value from timer_hour in my database.
var m = //I'm pulling this value from timer_minutes in my database.
var s = //I'm pulling this value from timer_seconds in my database.

//Uses the values from the database to set the timer.

//Add leading zero.
function HHMMSS(digits){
var HHMMSSstring=(digits.toString().length==1)? "0"+digits : digits;
return HHMMSSstring;

//Run the timer in a loop and display it.
function displayTimer1(){
var timeString1 = HHMMSS(timeLeft1.getHours())+":"+HHMMSS(timeLeft1.getMinutes())+":"+HHMMSS(timeLeft1.getSeconds());
t=setTimeout("displayTimer1()", 998);

document.getElementById("timer1").innerHTML= timeString1;

if (timeString1=="00:00:00"){stopCount();}

//Reduce the seconds by 1 every time this function loops.

/*I've tried a few methods to UPDATE the "timer_*" fields but here is my latest attempt - sending the new timeLeft1 values to hidden inputs on the page. Like I said, it doesn't work but it should at least illustrate what I'm trying to do. These ID's are defined on the page.*/
document.getElementById("hours1").innerHTML= timeLeft1.getHours();
document.getElementById("minutes1").innerHTML= timeLeft1.getMinutes();
document.getElementById("seconds1").innerHTML= timeLeft1.getSeconds();

//Send new timeLeft1 values back to the server. This is giving me a syntax error.
$con = mysql_connect("servername","username","password");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);

$hours = "getElementById('hours1')"
$minutes = "getElementById('minutes1')"
$seconds = "getElementById('seconds1')"

mysql_query("UPDATE tablename
SET timer_hour='$hours', timer_minutes='$minutes', timer_seconds='$seconds'
WHERE pod='1'");


//Stop the displayTimer1() loop.
function stopCount(){

The reason I'm using my database to house the h,m,s values is that the user will be able to change the countdown time (timeLeft1) and that new time should show for all other users. That's my end goal, so if I'm going about it the wrong way then I'm open for suggestions.

Thanks for taking a look!
8:14 am on June 21, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 9, 2009
posts: 43
votes: 0

After re-reading my post above I don't think I was very clear about what I'm trying to do; sorry about that. Hopefully this will explain a bit better (and briefer).

Simply put, I need to send the hours, minutes and seconds values (contained in my timeLeft1 variable) to corresponding fields in a database table.

UPDATE tablename
SET timer_hour='timeLeft1.getHours()', timer_minutes='timeLeft1.getMinutes()', timer_seconds='timeLeft1.getSeconds()'
WHERE pod='1'

Any suggestions would be greatly appreciated. Thanks!