homepage Welcome to WebmasterWorld Guest from 54.167.249.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
A mysql query("UPDATE") Problem
bsbarker




msg:4155603
 4:16 pm on Jun 20, 2010 (gmt 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:
timeLeft1.setHours(h,m,s);

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.
timeLeft1.setHours(h,m,s);

//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.
timeLeft1.setSeconds(timeLeft1.getSeconds()-1);

/*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.
<?php
$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'");

mysql_close($con);
?>
}

//Stop the displayTimer1() loop.
function stopCount(){
clearTimeout(t);
}

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!

 

bsbarker




msg:4155882
 8:14 am on Jun 21, 2010 (gmt 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.

Basically:
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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved