Jump to content

How to update the time in a database? (With MySQL and PHP)


Stalingrad

Recommended Posts

Hi! I have created a script which is not yet finished. I am just about done with it except for about three parts, and one of the parts of it is the part with the time. I want it be able to function, so when a user presses the "submit" button, the time starts at 59 minutes, and automatically updates. If the time runs out, then they are out of time and have to start over. If they can complete the task in under 59 minutes, then I would like the time to be displayed... like, how much time it took and how long they had left. How would I be able to do this? I know of cron jobs, but I would rather not use cron jobs for this.. I'm pretty sure there are other ways around doing this that are quite simple, but I'm just not understanding it. I'm not really good with PHP time and date yet. I have to use my php books to help me. If any body can help me with this I would very much appreciate it! I will post some code below explaining it as well. :)

 

I am using this date function for the minutes:

$left = date('i');[/code']

I will now post the entire code for the page I need this to work on.

[code]<?php
session_start();
include("FILE");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<?php
if(!isset($_SESSION['username'])) {
echo "<banner></banner><nav>$shownavbar</nav><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content>";
}

if(isset($_SESSION['username'])) {
echo "<nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>Train Your Pets</font><br><br><img src=/images/items/3.gif><img src=/images/items/1.gif><img src=/images/items/2.gif><br><br>Hello there! This is the place where you can train your pet! Each training session will cost you a magical <b>Gem</b>. There are a few different types of gems, and the gem you need will be picked randomly by me.<br><br>";
$traintr = date('i');
$traints = date('i');

$submit = $_POST['submit'];
$course = $_POST['train'];
$pet = $_POST['pet'];
$haveyyu = $_POST['have'];
$gem = rand(1, 3);

if(!isset($submit)) {
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Please Select a pet to Train: <select name="pet"><?php
$gtq = "SELECT * FROM userpets WHERE owner='$showusername'";
$tq = mysql_query("$gtq");
while($t = mysql_fetch_array($tq)) {
$tpm = $t['userpetname'];
$ts = $t['strength'];
$td = $t['defence'];
$th = $t['health'];
$tl = $t['level'];

echo "<option>$tpm</option>";
}
?>
</select><br>
<br>What would you like to train? <select name="train"><option>Strength</option><option>Defence</option><option>Health</option><option>Level</option></select><br><br><br><input type="submit" name="submit" value="Train Pet">
<?php
}
if(isset($submit)) {
$cinqe = mysql_query("SELECT * FROM train WHERE pet='$pet'");
$cinq = mysql_num_rows($cinqe);

if($cinq == 1) {
$lgiq = "SELECT * FROM train WHERE owner='$showusername'";
$lqi = mysql_query($lgiq);
while($lrow = mysql_fetch_array($lqi)) {
$rgem = $lrow['gem'];
}
echo "<font color=red>Error! Your pet is already on a training course!</font><br><br>It will end in <b>59</b> minutes.<br><br>";

}
if($cinq == 0) {
$getthisq = "SELECT * FROM train WHERE owner='$showusername'";
$getthis = mysql_query("$getthisq");
while($prow = mysql_fetch_array($getthis)) {
$kname = $prow['pet'];
}
mysql_query("INSERT INTO `train`(`owner`, `pet`, `left`, `stat`, `complete`, `gem`) VALUES ('$showusername','$pet','59','$train','No','$gem')");
mysql_query("UPDATE users SET credits=credits-150 WHERE username='$showusername'");
mysql_query("UPDATE userpets SET $thestat=$thestat+1 WHERE userpetname=$thepetname'");
echo "<font color=green>Success! Your pet is now on a training course!<br><br></font>The course will end in <b>59</b> minutes.<br><br>";
}
}
}

?>

</html>

 

Basically, I want the database to update the stats once they complete what they need to do, if they can do it under the 59 minutes. I haven't completed that parts yet, though. How would I go about doing that? Would I have to create a new table for the time, or can I just extend the "train" table and add  new columns to it for the time part? Thank you in advance!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.