gpt4 book ai didi

php - Mysql 将 PHP 变量保存到表中

转载 作者:行者123 更新时间:2023-11-29 16:01:52 25 4
gpt4 key购买 nike

我有一个非常具体的问题

我想将两个变量保存/加载到数据库,第三个变量用作标识符

我当前的-不工作-代码:

$sql = mysql_query("INSERT INTO time (meno, minuty, sekundy) VALUES('$firstName','$minutes','$seconds')");
if (mysql_error()) die('Error, insert query failed');

简而言之,我想要的:当我使用名称(等Roman [$firstName变量])登录时,它将加载之前的$分钟和$秒数字,并保存每(等分钟)新的数字(它是一个定时器,这样可以节省时间)

希望你能理解

感谢您的宝贵时间,我很感激

我当前的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);
}
}

?>

最佳答案

请创建两个表,一个表用于保存两个变量,另一个表用于保存标识符。然后使用外键和 JOINS 关系。希望这些步骤能够解决您的问题。

关于php - Mysql 将 PHP 变量保存到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com