php连接mysql,php网页实时显示数据库数据(3s刷新一次),可操作删除整个数据库或单条数据
1.界面样式
2.原理框图(笔者理解,欢迎指正)
3.程序代码 temp.php del.php clean.php
#####del.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>删除记录</title>
</head>
<body>
<?php
$link = mysqli_connect('localhost','root','XXXXXXXXXX','temp');
if (!$link){
exit('数据库连接失败!');
}
session_start();
$del = $_SESSION['del'];
mysqli_query($link,"delete from tempdataz where id = $del ");
unset($_SESSION['del']);
header('location:temp.php');
?>
</body>
</html>
####temp.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>检测结果-temp.php</title>
</head>
<body>
<h1 align="center">温度信息</h1>
<form action="" method="post" name="indexf">
<p align="center"><input type="button" value="清除" name="cleanbut" onClick="location.href='clean.php'" /></p>
<table align="center" border="1px" cellspacing="0px" width="800px">
<tr><th>时间</th><th>温度℃</th><th>操作</th></tr>
<?php
header("refresh: 3");
session_start();
if (isset($_SESSION['yes'])){
echo '<p align="center">'.$_SESSION['yes'].'</p>';
unset($_SESSION['yes']);
}
$link = mysqli_connect('localhost','root','XXXXXXXXXXX','temp');
if (!$link){
exit('数据库连接失败!');
}
$res = mysqli_query($link,"select * from tempdataz order by time");//全结果集
while ($row = mysqli_fetch_array($res)){
echo '<tr align="center">';
echo "<td>$row[1]</td><td>$row[2]</td>
<td>
<input type='submit' name='delsub$row[0]' value='删除' />
</td>";
echo '</tr>';
if (!empty($_POST["delsub$row[0]"])){
$_SESSION['del'] = $row[0];
echo '<script>
if (confirm("是否删除?") == true){
location.href="del.php";
}
</script>';
}
}
mysqli_close($link);
?>
</table>
</form>
</body>
</html>
####clean.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>清除数据记录</title>
</head>
<body>
<h1 align="center">警告!</h1>
<form action="" method="post" name="inf">
<p align="center">是否清除数据</p>
<p align="center"><input type="submit" name="insub" value="是" /></p>
</form>
<?php
session_start();
$link = mysqli_connect('localhost','root','XXXXXXXXXX','temp');
if (!$link){
exit('数据库连接失败!');
}
if (!empty($_POST["insub"])){
mysqli_query($link,"TRUNCATE TABLE tempdataz;");
$_SESSION['yes'] = '清除成功!';
header('location:temp.php');
}
?>
</body>
</html>