MySQL Forums
Forum List  »  PHP

Add "now" time from form to DB Problem
Posted by: Ken Rogers
Date: May 06, 2016 07:00AM

I have created a PHP form that we use on a tablet for what is called check times while scoring the finish of running races. The way it works is that as a runner crosses the finish line the user hits the Finish Time button which inserts the time into the “time” input box. Then they enter a bib number into the “bib” input box and the tap the “ENTER” button which adds the time and bib to the MySQL database.

All of this pretty much works OK except for this: When the user hits Finish Time button and populates the time input form with the current time (which is correct and completely synced with the other machines), when you hit enter and transfer the info to MySQL what is being transferred is the time the user hits Enter, not the time that is in the box. So if it takes the user 5 seconds to add the bib number then the time that is submitted is five seconds after the time that was seen in the Time input form.

I am a bit of a newbie to PHP so I am struggling to figure out a solution. Here is the entire PHP page:

<?php

// include_once "add-record-form.php;

$link = mysqli_connect("192.168.2.90", "user", "password", "running");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$bib = mysqli_real_escape_string($link, $_POST['bib']);
$time = mysqli_real_escape_string($link, $_POST['time']);

date_default_timezone_set('America/Denver');
$time = date("Y-m-d H:i:s");


// attempt insert query execution
$sql = "INSERT INTO checktimes (bib, time) VALUES ('$bib', '$time')";
if(mysqli_query($link, $sql)){
echo "-";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

//$sql2 = "SELECT bib, time FROM checktimes";
//$result = $link->query($sql2);

//if ($result->num_rows > 0) {
// output data of each row
//while($row = $result->fetch_assoc()) {
// echo "id: " . $row["id"]. " - bib: " . $row["bib"]. " " . $row["time"]. "<br>";
//}
//} else {
//echo "0 results";
//}

// close connection
//mysqli_close($link);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
<script>

function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}

function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}



</script>

</head>

<body onload="startTime()" onLoad="clearform()" onUnload="clearform()">

<div id="txt"></div>

<style>
.hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>

<iframe name="hiddenFrame" class="hide"></iframe>

<!--<form name="myform" action="insert.php" method="post">>-->
<form name="myform" method="post" action="<?php echo $_SERVER[ 'PHP_SELF' ]; ?>">
<p>
<button type="button" style="height:150px; width:150px; font-size: larger; color: teal; background-color: #FFFFC0; border: 3pt ridge lightgrey" onclick="this.form.time.value=getTimeStamp();document.myform.bib.focus()">Finish Time</button>
<input type="text" name="time" size="20">

<label for="bib">Bib #:</label>
<input type="number" name="bib" id="bib" style="height:25px; width:50px">

<button type="submit" style="height:150px; width:150px; font-size: larger; color: teal; background-color: #FFFFC0; border: 3pt ridge lightgrey">ENTER</button>
</p>

</form>
</body>
</html>

Options: ReplyQuote


Subject
Written By
Posted
Add "now" time from form to DB Problem
May 06, 2016 07:00AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.