您的位置:首页 > 编程语言 > PHP开发

php留言本

2015-10-24 10:50 627 查看
<!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
d891
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