编辑之后变成了 添加数据
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$editkey=$_GET['editkey'];
$filename="msg.txt";
$msgs=[];
//检测文件是否存在
if(file_exists($filename)){
//读取文件中的内容
$string=file_get_contents($filename);
if(strlen($string)>0){
$msgs=unserialize($string);
}
}
//检测用户是否点击了提交按钮
/*$editarr = $msgs[$editkey];
print_r($editarr);
if(isset($_POST['pubMsg'])){
$msgs[$editkey]["editkey"]=$_POST['editkey'];
$msgs[$editkey]["username"]=$_POST['username'];
$msgs[$editkey]["title"]=strip_tags($_POST['title']);
$msgs[$editkey]["content"]=strip_tags($_POST['content']);
$msgs[$editkey]["time"]=time();
//array_push($msgs,$data);
$key = $_POST['editid'];
$msgs[$key] = $data;
$msgs=serialize($msgs);//serialize — 产生一个可存储的值的表示
if(file_put_contents($filename,$msgs)){
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('编辑失败!');location.href='index.php';</script>";
}
}
if(is_array($editkey)||count($editarr)<=0){
echo "<script>alert('留言出错,无法编辑!');location.href='index.php';</script>";
}
*/
//检测用户是否点击了提交按钮
if(isset($_POST['pubMsg'])) {
$editkey = $_POST['editkey'];
$username = $_POST['username'];
$title = $_POST['title'];
$content = $_POST['content'];
$time = time();
//将其组成关联数组
$data = compact('username', 'title', 'time', 'content');
$msgs[$editkey] = $data;
$msgs = serialize($msgs);
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<form action="edit.php" method="post">
<fieldset>
<legend>请留言</legend>
<label>用户名</label><input type="text" name="username" required value="<?php echo $msgs[$editkey]['username']; ?>"/>
<label>标题</label><input type="text" name="title" required value="<?php echo $msgs[$editkey]['title']; ?>" />
<label>内容</label><textarea name="content" rows="5" cols="30" required><?php echo $msgs[$editkey]['content']; ?></textarea>
<hr>
<input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="编辑完成"/>
<a href="index.php"><input type="submit" class="btn btn-primary btn-lg" value="查看留言"/></a>
<input name="edit" value="<?php echo $editkey ?>">
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
首页
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=[];
//检测文件是否存在
if(file_exists($filename)){
//读取文件中的内容
$string=file_get_contents($filename);
if(strlen($string)>0){
$msgs=unserialize($string);
}
}
?>
<!DOCTYPE html>
<html>
<html>
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
留言板-<small>V1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello
</h1>
<p>
来了就说两句吧
</p>
</div>
<?php if(is_array($msgs)&&count($msgs)>0):?>
<table class="table">
<thead>
<tr>
<th>
编号
</th>
<th>
用户
</th>
<th>
标题
</th>
<th>
时间
</th>
<th>
内容
</th>
<th>
操作
</th>
</tr>
</thead>
<body>
<?php foreach($msgs as $key=>$val):?>
<tr class="success">
<td>
<?php echo $key;?>
</td>
<td>
<?php echo $val['username'];?>
</td>
<td>
<?php echo $val['title'];?>
</td>
<td>
<?php echo date("m/d/Y H:i:s",$val['time']);?>
</td>
<td>
<?php echo $val['content'];?>
</td>
<td>
<a href="edit.php?editkey=<?php echo $key ?>">编辑</a>|
<a href="delete.php?delkey=<?php echo $key?>">删除</a>
</td>
</tr>
<?php endforeach;?>
</body>
</table>
<?php endif;?>
<input type="submit" class="btn btn-primary btn-lg" value="我要留言" onclick="window.location.href='add.php'"/>
<hr/>
</div>
</div>
</div>
</body>
</html>
正在回答 回答被采纳积分+1
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$editkey=$_GET['editkey'];
$filename="msg.txt";
$msgs=[];
//检测文件是否存在
if(file_exists($filename)){
//读取文件中的内容
$string=file_get_contents($filename);
if(strlen($string)>0){
$msgs=unserialize($string);
}
}
//检测用户是否点击了提交按钮
if(isset($_POST['pubMsg'])) {
$username = $_POST['username'];
$title = $_POST['title'];
$content = $_POST['content'];
$time = time();
//将其组成关联数组
$data = compact('username', 'title', 'time', 'content');
$msgs[$editkey] = $data;
$msgs = serialize($msgs);
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<form action="edit.php" method="post">
<fieldset>
<legend>请留言</legend>
<label>用户名</label><input type="text" name="username" required value="<?php echo $msgs[$editkey]['username']; ?>"/>
<label>标题</label><input type="text" name="title" required value="<?php echo $msgs[$editkey]['title']; ?>" />
<label>内容</label><textarea name="content" rows="5" cols="30" required><?php echo $msgs[$editkey]['content']; ?></textarea>
<hr>
<input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="编辑完成"/>
<a href="index.php"><input type="submit" class="btn btn-primary btn-lg" value="查看留言"/></a>
<input name="edit" value="<?php echo $editkey ?>">
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星