使用云上托管RDS部署LAMP demo
能插入数据并实时显示
资源清单
一、代码
1. index.php
<html>
<head>
<title>Hands-on Lab 动手实验室</title>
<link rel="stylesheet" href="./src/main.css">
</head>
<body>
<?php
ini_set("display_errors","On");
$host = "";
$db_account = "";
$db_password = "";
$db_connect = mysqli_connect($db_ip,$db_account,$db_password);
mysqli_query($db_connect,"use lab;");
$db_query = mysqli_query($db_connect,"SELECT * FROM myapp;");
$data = array();
while($result = mysqli_fetch_array($db_query)){
$data[] = $result;
}
?>
<div class="content">
<div class="title">Hands-on Lab Timeline</div>
</div>
<div class="insert_block">
<fieldset>
<legend>添加数据</legend>
<form action="./request.php" method="post">
<div class="row"><input type="text" name="name" value="" placeholder="请填写姓名,例如:zhangsan" /></div>
<div class="row"><input type="text" name="lab" value="" placeholder="请填写实验名称,例如:Lab1" /></div>
<div class="row"><button type="submit">增加数据</submit></div>
</form>
</fieldset>
</div>
<div class="content">
<div class="title">Timeline 提交记录</div>
<div class="sub_title">
<?php
$hostname = exec("hostname",$b);
echo "ECS hostname: ".$hostname;
?>
</div>
<table>
<tr class="table_header">
<td width="28">序号</td>
<td width="100">时间</td>
<td width="100">实验</td>
<td width="100">同学</td>
</tr>
</tr>
<?php
$index = 1;
foreach($data as $item){
?>
<tr>
<td><?php echo $index++;?></td>
<td><?php echo @date("Y-m-d H:i:s",$item["created_at"]+8*3600);?></td>
<td><?php echo $item["lab"]?></td>
<td><?php echo $item["name"]?></td>
</tr>
<?php }?>
</table>
</div>
</body>
</html>
2. Request.php
<?php
$data = $_POST;
$host = "";
$account = "";
$password = "";
$db = "lab";
$a = mysqli_connect($host,$account,$password);
mysqli_query($a,"use lab;");
$name = $data["name"];
$lab = $data["lab"];
$created_at = time();
$sql = "INSERT INTO myapp(name,lab,created_at) value('".$name."','".$lab."','".$created_at."');";
mysqli_query($a,$sql);
?>
<script>window.location.href="index.php"</script>
3. main.css (./src)
body {
font-size: 14px;
}
.insert_block {
width:1200px;
margin:0px auto 20px;
border:0px solid #CCCCCC;
box-shadow: 5px 5px 10px #CCCCCC;
padding:20px;
}
.row {
padding: 6px 0;
}
input {
padding: 6px 10px;
margin: 10px 20px;
width: 320px;
font-size: 14px;
}
button {
margin-left: 20px;
background-color: #FF8800;
color: #FFFFFF;
border: none;
padding: 10px 0px;
width: 320px;
}
fieldset {
border: 1px solid #FF8800;
}
.content {
width: 1200px;
margin: 0 auto;
padding: 28px 0;
}
.title {
padding: 0px 0 20px 0;
font-size: 20px;
}
table {
width: 100%;
font-size:14px;
line-height:28px;
}
.table_header {
background-color:RGB(245,249,255);
}
.sub_title {
padding: 0px 0 20px 0;
font-weight:bold;
}
二、资源配置
云主机
RDS
效果
其他
https://www.php.cn/php-weizijiaocheng-226396.html