PHP 体重管理网页 - 记录你的体重变化趋势
<!DOCTYPE html>
<html>
<head>
<title>PHP 体重管理网页</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
}
form {
margin-bottom: 20px;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
#chart {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<h1>PHP 体重管理</h1>
<pre><code><?php
// 数据库连接信息
$servername = '103.214.173.233';
$username = 'water';
$password = 'xX300400';
$dbname = 'water';
// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die('连接失败:' . $conn->connect_error);
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$weight = $_POST['weight'];
$timestamp = time();
// 插入数据
$sql = 'INSERT INTO tizhongrou (data, tizhong) VALUES ($timestamp, $weight)';
if ($conn->query($sql) === TRUE) {
echo '体重已上传成功!';
} else {
echo '错误:' . $sql . '<br>' . $conn->error;
}
}
// 查询历史体重数据
$sql = 'SELECT data, tizhong FROM tizhongrou';
$result = $conn->query($sql);
$chartData = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$chartData[] = [$row['data'] * 1000, $row['tizhong']];
}
}
$conn->close();
?>
<!-- 录入体重表单 -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<label for="weight">体重:</label>
<input type="number" step="0.1" name="weight" required>
<input type="submit" value="提交">
</form>
<!-- 折线图 -->
<div id="chart"></div>
<script src="https://cdn.jsdelivr.net/npm/apexcharts@3.28.0"></script>
<script>
var options = {
series: [{
name: '体重',
data: <?php echo json_encode($chartData); ?>
}],
chart: {
height: 400,
type: 'line',
zoom: {
enabled: false
}
},
xaxis: {
type: 'datetime'
}
};
var chart = new ApexCharts(document.querySelector('#chart'), options);
chart.render();
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/baTp 著作权归作者所有。请勿转载和采集!