<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>留言本</title> </head> <body> <form action="write.php" method="post"> <p>留言标题:<input type="text" name="title" /></p> <p>留言内容:<textarea name="content" id="" cols="30" rows="10" > </textarea></p> <p><input type="submit" value="提交" /></p> </form> </body></html>
-----------------write.php-------------
<?php //print_r($_POST); $fh=fopen('./msg.txt','a'); $str=$_POST['title'].','.$_POST['content']."\n"; fwrite($fh,$str); fclose($fh); echo 'ok';-------------msg.php--------------------
<?php $fh=fopen('./msg.txt','r'); $i=1; while($row=fgetcsv($fh)){ echo '<li>'.'<a href=./readmsg.php?tid=',$i,' />'.$row[0].'</li>'; $i++; }------------------readmsg.php--------------
<?php $tid=$_GET['tid']; $fh=fopen('./msg.txt','r'); $i=1; while($row=fgetcsv($fh)){ if($i==$tid){ print_r($row); } $i++; }
**********************数据库形式*********************
---------------write.php------------------
<?php $conn = mysqli_connect("localhost", "root", "123456", "test"); if (!$conn) { die("连接失败" . mysql_error()); } else { echo "Success!"; } mysqli_query($conn, "set names utf8"); $sql="insert into message (title,content,pubtime) VALUES ('".$_POST['title']."', '".$_POST['content']."',".time().")"; echo $sql."<br/>"; if($res = mysqli_query($conn, $sql)){ echo "留言成功"; }else{ echo "留言失败"; } ?>---------------------msg.php---------------------------
<?php $conn = mysqli_connect("localhost", "root", "123456", "test"); mysqli_query($conn, "set names utf8"); $sql="select * from message"; $res = mysqli_query($conn, $sql); //var_dump($res); while($row=mysqli_fetch_assoc($res)){ //print_r($row); echo '<li>','<a href=readmsg.php?id=',$row['id'],' />',$row['title'].'</li>'; }-----------------------readmsg.php-----------------------
<?php $conn = mysqli_connect("localhost", "root", "123456", "test"); mysqli_query($conn, "set names utf8"); $id=$_GET['id']; $sql="select * from message where id=".$id; $res = mysqli_query($conn, $sql); $row=mysqli_fetch_assoc($res); ?> <h1><?php echo $row['title'];?></h1> <p><?php echo $row['content'];?></p>
php留言本
最新推荐文章于 2023-04-09 00:21:03 发布