php留言板小demo
测试环境
- 本地数据库名:
php10
user:root
,passwd:
,
- 表:
msg
,4个字段id
,content
,user
, intime
一些简单sql语句
增: INSERT INTO msg () VALUES ();
删: DELETE FROM msg WHERE id = 10;
DELETE FROM msg
改: UPDATE msg SET content='xx',user='xx'
UPDATE msg SET content='xx',user=xx WHERE id<1 and id>9
查 : SELECT * FROM msg
(*
代表字段)
MYSQLI 函数的使用
test1.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <?php
$host='127.0.0.1'; $dbuser='root'; $passwd=''; $database='php10';
$db = new mysqli($host,$dbuser,$passwd,$database);
if ($db->connect_errno) die("数据库连接失败");
$db->query("SET NAMES UTF8"); $sql = 'select * from msg order by id desc;';
$myquery_result = $db->query($sql);
if (!$myquery_result) die("查询失败");
$rows = []; while ( $row = $myquery_result->fetch_array(MYSQLI_ASSOC) ) { $rows[] = $row; }
?>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>留言本</title> <style> .warp { width:600px; margin:0px auto; } .add { overflow: hidden;} .add .content { width:598px; padding:0; margin:0; } .add .user {float:left;} .add .btn {float:right;}
.msg {margin:20px 0px; background: .msg .info {overflow:hidden;} .msg .user {float:left; color:blue;} .msg .time {float:right; color:black;} .msg .content { width:100%; padding:5px; } </style> </head> <body> <div class='warp'> <div class='add' > <!-- 表单提交给 formsave.php, 通过 formsave.php 将表单数据写入数据库 --> <form action="formsave.php" method="post"> <textarea name='content' class='content' cols='50' rows='5'> </textarea> <input name='user' class='user' type='text'/> <input class='btn' type='submit' value='发表留言' /> </form> </div> <?php // 通过遍历显示留言内容 foreach( $rows as $row) { ?> <!-- 留言板显示 --> <div class='msg'> <div class='info'> <span class='user'> <?php echo $row['user']; ?> </span> <span class='time'> <?php echo date("Y-m-d H:i:s",$row['intime']); ?> </span> </div> <div class='content'> <span> <?php echo $row['content']; ?> </span> </div> </div> <?php } ?> </div> </body> </html>
|
formsave.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <?php
$content = $_POST['content']; $user = $_POST['user'];
function post( $content ) { if ( $content == '') return false; $forbid_use = ['xx','xx','xx','xx'];
foreach($forbid_use as $i) if ($content == $i) return false; return true; } if ( !post($content) ) die ('留言不正确<br>');
if ( !post($user) ) die ('用户不正确<br>');
$host = '127.0.0.1'; $dbuser = 'root'; $passwd = ''; $database = 'php10';
$db = new mysqli($host,$dbuser,$passwd,$database); $db->query("SET NAMES UTF8");
if ($db->connect_errno) die('连接数据库失败');
$time = time();
$sql = "insert into msg (content,user,intime) values ('{$content}','{$user}',$time);";
$flag = $db->query( $sql );
if (!$flag) die("插入数据失败");
header("location: test1.php"); ?>
|