我有一个非常具体的问题
我想将两个变量保存/加载到数据库中,而第三个变量用作标识符
我当前无法正常工作的代码
$sql = mysql_query("INSERT INTO time (meno, minuty, sekundy) VALUES('$firstName','$minutes','$seconds')");
if (mysql_error()) die('Error, insert query failed');
简而言之,我想要的是:当我使用名称登录时(例如Roman [$ firstName variable]),它将加载先前的$ minutes和$ seconds数字,并保存每隔(等分)新的数字(计时器,这样可以节省时间)
希望你能理解
感谢您的宝贵时间,我很珍惜
我当前的timer.php
<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=Windows-1250" />
<title>Timing Stránka</title>
<script>
let startTime, endTime;
$(window).on('load', () => {
startTime = new Date();
});
function time_elapsed() {
endTime = new Date();
let timeDiff = endTime - startTime;
let timeSpent = timeConversion(timeDiff);
const formData = new FormData();
formData.append('timeSpent', timeSpent);
/* The line below is used to send data to the server-side. This way is reliable than using AJAX to send the data especially in cases when you are listening for an unload event. You can read more about navigator.sendBeacon() in MDN's site. */
navigator.sendBeacon('db.php', formData);
}
function timeConversion(time) {
let seconds = (time / 1000).toFixed(1);
let minutes = (time / (1000 * 60)).toFixed(1);
let hours = (time / (1000 * 60 * 60)).toFixed(1);
let days = (time / (1000 * 60 * 60 * 24)).toFixed(1);
if (seconds < 60) {
return seconds + " second(s)";
} else if (minutes < 60) {
return minutes + " minute(s)";
} else if (hours < 24) {
return hours + " hour(s)";
} else {
return days + " day(s)";
}
}
/* Note: In the line below, i listen to the unload event, you can change this event to a button click or anything else you want to listen to before calling the function. This is better than calling setInterval() every second and i think it will help your application performance also. */
window.addEventListener('beforeunload', time_elapsed, false);
</script>
</head>
<body>
</div>
</br>
</br>
</br>
<?php
echo $timeSpent
?>
还有db.php:
<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
// DB connection
$host = 'db.mysql-01.gsp-europe.net';
$db_name = 'xxxx';
$username = 'xxx';
$password = 'xxxx';
try {
$conn = new PDO('mysql:host='.$host.';dbname='.$db_name, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection Error: " . $e->getMessage();
}
if (isset($_POST['timeSpent'])){
$timeSpent = $_POST['timeSpent'];
// create query
$query = 'INSERT INTO user_time SET time = :time';
// prepare statement
$stmt = $conn->prepare($query);
// bind data
$stmt->bindParam(':time', $timeSpent);
// execute query and check if it failed or not
if ($stmt->execute()){
echo "Query Successful";
} else {
printf("Error: %s.\n", $stmt->error);
}
}
?>
答案 0 :(得分:0)
请创建两个表,一个表用于保存两个变量,另一个表用于保存标识符。然后使用外键和JOINS关系。希望这些步骤可以解决您的问题。
答案 1 :(得分:0)
请用以下代码替换“ time.php”中的代码:
<?php
header('Content-Type: text/html; charset=Windows-1250');
session_start();
$firstName = $_SESSION['firstname'];
$minutes = $_POST['minutes'];
$seconds = $_POST['seconds'];
// DB connection
$host = 'localhost';
$db_name = 'zadmin';
$username = 'xxx';
$password = 'zadmin_nahovno';
try {
$conn = new PDO('mysql:host='.$host.';dbname='.$db_name, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection Error: " . $e->getMessage();
}
// create query
$query = 'INSERT INTO time SET meno = :firstName, minuty = :minutes, sekundy = :seconds';
// prepare statement
$stmt = $conn->prepare($query);
// bind data
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':minutes', $minutes);
$stmt->bindParam(':seconds', $seconds);
// execute query and check if it failed or not
if ($stmt->execute()){
echo "Query Successful";
} else {
printf("Error: %s.\n", $stmt->error);
}
?>
/*
This should work (if not, then something is wrong with your variables, you should look into your variables and see if they are actually holding any data). You can make use of var_dump() to examine the variables.
*/
Meno Užívateľa: <b> <?php echo $firstName; ?> </b>
</br>
</br>
Momentálne majníš : <b> <?php echo $minutes; ?> Minút </b> <b> a </b> <b> <?php echo $seconds; ?> Sekúnd </b>
</br>
</br>
答案 2 :(得分:0)
@Hnusny Pleb,所以为了获得在页面上花费的时间,我为您编写了以下代码。
首先,在脚本中,您应该这样写:
<script>
let startTime, endTime;
$(window).on('load', () => {
startTime = new Date();
});
function time_elapsed() {
endTime = new Date();
let timeDiff = endTime - startTime;
let timeSpent = timeConversion(timeDiff);
const formData = new FormData();
formData.append('timeSpent', timeSpent);
/* The line below is used to send data to the server-side. This way is reliable than using AJAX to send the data especially in cases when you are listening for an unload event. You can read more about navigator.sendBeacon() in MDN's site. */
navigator.sendBeacon('index.php', formData);
}
function timeConversion(time) {
let seconds = (time / 1000).toFixed(1);
let minutes = (time / (1000 * 60)).toFixed(1);
let hours = (time / (1000 * 60 * 60)).toFixed(1);
let days = (time / (1000 * 60 * 60 * 24)).toFixed(1);
if (seconds < 60) {
return seconds + " second(s)";
} else if (minutes < 60) {
return minutes + " minute(s)";
} else if (hours < 24) {
return hours + " hour(s)";
} else {
return days + " day(s)";
}
}
/* Note: In the line below, i listen to the unload event, you can change this event to a button click or anything else you want to listen to before calling the function. This is better than calling setInterval() every second and i think it will help your application performance also. */
window.addEventListener('beforeunload', time_elapsed, false);
</script>
在编写完上面的脚本之后,数据将被发送到您的服务器端,然后您可以简单地将花费的时间(即秒,分钟,小时或天)存储到数据库中。为此,您应该在服务器端编写类似于以下内容的内容:
<?php
if (isset($_POST['timeSpent'])){
$timeSpent = $_POST['timeSpent'];
// create query
$query = 'INSERT INTO user_time SET time = :time';
// prepare statement
$stmt = $conn->prepare($query);
// bind data
$stmt->bindParam(':time', $timeSpent);
// execute query and check if it failed or not
if ($stmt->execute()){
echo "Query Successful";
} else {
printf("Error: %s.\n", $stmt->error);
}
}
?>
请找到一种使用书面代码实现目标的方法。我认为香港专业教育学院竭尽所能帮助您。祝你好运。
答案 3 :(得分:0)
Okey,我知道了。这是代码:
定时页面:
<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
session_start();
$_SESSION['firstname'] = $firstName;
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=Windows-1250" />
<title>Timing Stránka</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('db.php').fadeIn("slow");
}, 1000); // refresh every 10000 milliseconds
</script>
</head>
<body>
</div>
</br>
</br>
</br>
<div id="load_tweets"> </div>
Time on page: <label id="minutes">00</label>
<label id="colon">:</label>
<label id="seconds">00</label>
<script type="text/javascript">
var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);
function setTime()
{
++totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds%60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds/60));
}
function pad(val)
{
var valString = val + "";
if(valString.length < 2)
{
return "0" + valString;
}
else
{
return valString;
}
}
</script>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Oprava">
</body>
</html>
AKA db.php计时页面:
<?php
header('Content-Type: text/html; charset=Windows-1250');
session_start();
$firstName = $_SESSION['firstname'];
$_SESSION['firstname'] = $firstName;
$servername = "db.xxxx.gsp-europe.net";
$username = "xxxxxx";
$password = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
$dbname = "xxxxx";
/// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
$stmt = $conn->prepare("SELECT points FROM member_profile WHERE user_id = '$firstName'");
$stmt->execute();
$array = [];
$resalts = $stmt->get_result();
while ($row = $resalts->fetch_array(MYSQLI_ASSOC))
{
$points = $row['points'];
}
$hours = floor($points / 3600);
$mins = floor($points / 60 % 60);
$secs = floor($points % 60);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// check if the user exist
$check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";
$result = mysqli_query($conn,$check) or die(mysqli_error($conn));
$rows = mysqli_num_rows($result);
//if exist increse points with 1
if($rows>=1){
$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error doing sum thingz: " . $conn->error;
}
}
//if don't exist create user with points 0
if($rows==0)
{
$query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";
$result = mysqli_query($conn,$query)or die(mysqli_error($conn));
$conn->close();
}
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=Windows-1250" />
</head>
<body>
</div>
</br>
Meno Užívateľa: <b> <?php echo $firstName; ?> </b>
</br>
</br>
Overall time : <b> <?php echo $timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs); ?> </b>
</body>
</html>