编辑功能只能编辑第二条数据,麻烦老师帮忙看一下
index.php文件
<?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);//反序列化字符串
}
}
//检测用户是否点击了提交按钮
if(isset($_POST['leaveMsg'])){
$username = $_POST['username'];
$title = strip_tags($_POST['title']);//过滤留言信息中的HTML和PHP标签
$content = strip_tags($_POST['content']);
$time = time();
//将其组成关联数组
$data = compact('username', 'title', 'content', 'time');
array_push($msgs, $data);
$msgs = serialize($msgs);//序列化字符串
}
if(isset($_GET['delkey'])){
$key = $_GET['delkey'];
//要删除的数据不存在
if(!isset($msgs[$key])){
echo "<script>alert('数据出错,请重试!');location.href='index.php';</script>";
}
unset($msgs[$key]);
$msgs=serialize($msgs);
if(file_put_contents($filename,$msgs)){
echo "<script>alert('删除成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('删除失败,请重试!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>慕课留言板</title>
<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>
<script type="text/javascript">
function del(key){
if(confirm("确定删除?")){
location.href="delete.php?delkey="+key;
}else{
location.href="index.php";
}
}
</script>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
慕课留言板-<span>V1.0</span>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, World!
</h1>
<p>
一个自由,平等与民主的留言板, 你可以自由友善的发言。
</p>
<p>
<a rel="nofollow" class="btn btn-primary btn-large" href="#">查看更多 »</a>
</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>
<tbody>
<?php $i=1; foreach ($msgs as $key => $val){?>
<tr class="success">
<td>
<?php echo $i++;?>
</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?id=<?php echo $key ?>">编辑</a> |
<a href="javascript:;" onclick="del(<?php echo $key?>);return false;">删除</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php ; ?>
<form action="#" method="post">
<fieldset>
<input type="submit" class="btn leave message btn-lg" name="leaveMsg" value="我要留言">
</fieldset>
</form>
</div>
</div>
</div>
<hr>
<div class="footer">
<p><span>慕课商城</span>©2017 powered by IMooc.inc</p>
</div>
<!--跳转留言页面-->
<?php
if(isset($_POST['leaveMsg'])) {
$leaveMsg = $_POST['leaveMsg'];
header("Location: add.php");
}else{
exit;
}
?>
</body>
</html>
edit.php文件
<?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);//反序列化字符串
}
}
// 获取数组的键名
$editkey=$_GET['editkey'];
$editmsgs=$msgs[$editkey];
//判断是否点击编辑按钮
if (isset($_POST['editMsg'])) {
$editmsgs['username']=$_POST['username'];
$editmsgs['title']=$_POST['title'];
$editmsgs['content']=$_POST['content'];
$editmsgs['time']=time();
//将指定位置的数据修改为接收到的新数据。
$msgs[$editkey]=$editmsgs;
//将所有数据序列化
$msgs=serialize($msgs);
//向文件中重写内容
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('编辑失败!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>慕课留言板</title>
<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>
<style type="text/css">
form{
text-align: center;
}
button{
width: 82px;
height: 30px;
}
.check a:link{
color: #0C0C0C;
text-decoration: none;
}
.check a:visited{
color: #0C0C0C;
text-decoration: none;
}
.username{
width: 440px;
}
.title{
width: 440px;
}
.content{
width: 440px;
height: 220px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
慕课留言板-<span>V1.0</span>
</h1>
</div>
<div class="hero-unit">
<h1>
来都来了,就说点什么吧~~~~
</h1>
<p>
把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。
</p>
<p>
<a rel="nofollow" class="btn btn-primary btn-large" href="#">查看更多 ?</a>
</p>
</div>
<h3>编辑</h3>
<hr/>
<!--留言提交-->
<form action="edit.php" method="post">
<fieldset>
<label>用户名</label><input type="text" name="username" class="username" value="<?php echo $editmsgs['username'];?>" required>
<label>标题</label><input type="text" name="title" class="title" value="<?php echo $editmsgs['title'];?>" required>
<label>内容</label><textarea name="content" rows="5" cols="30" class="content" value="<?php echo $editmsgs['content'];?>" required></textarea><br/>
<input type="submit" class="btn btn-primary btn-lg" name="editMsg" value="编辑完成">
<button><div class="check"><a href="index.php">查看留言</a></div></button>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>



点击第四条数据后出现第二条数据的标题和用户名,没有内容。编辑内容后跳转index页后发现第二条数据被修改,添加数据和删除数据功能都没问题
3
收起
正在回答
4回答
同学你好,在edit文件中添加input标签,如下:
<input type="hidden" name="id" value="<?php echo $editkey ?>">
与form表单一起提交。然后在PHP代码中进接收。代码如下:
//判断是否点击编辑按钮
if (isset($_POST['editMsg'])) {
$data['username']=$_POST['username'];
$data['title']=$_POST['title'];
$data['content']=$_POST['content'];
$data['time']=time();
//将指定位置的数据修改为接收到的新数据。
$id = $_POST['id'];
$msgs[$id]=$data;
// var_dump($id);die;
//将所有数据序列化
$msgs=serialize($msgs);
//向文件中重写内容
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('编辑失败!');location.href='index.php';</script>";
}
}祝学习愉快!
断线纸鸢1
2019-08-18 12:03:47
edit.php 有die;只显示数组
<?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);//反序列化字符串
}
}
//获取数组的键名
$editkey=$_GET['id'];
$editmsgs=$msgs[$editkey];
var_dump($editmsgs);die;
//判断是否点击编辑按钮
if (isset($_POST['editMsg'])) {
$editmsgs['username']=$_POST['username'];
$editmsgs['title']=$_POST['title'];
$editmsgs['content']=$_POST['content'];
$editmsgs['time']=time();
//将指定位置的数据修改为接收到的新数据。
$msgs[$editkey]=$editmsgs;
//将所有数据序列化
$msgs=serialize($msgs);
//向文件中重写内容
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('编辑失败!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>慕课留言板</title>
<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>
<style type="text/css">
form{
text-align: center;
}
button{
width: 82px;
height: 30px;
}
.check a:link{
color: #0C0C0C;
text-decoration: none;
}
.check a:visited{
color: #0C0C0C;
text-decoration: none;
}
.username{
width: 440px;
}
.title{
width: 440px;
}
.content{
width: 440px;
height: 220px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
慕课留言板-<span>V1.0</span>
</h1>
</div>
<div class="hero-unit">
<h1>
来都来了,就说点什么吧~~~~
</h1>
<p>
把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。
</p>
<p>
<a rel="nofollow" class="btn btn-primary btn-large" href="#">查看更多 ?</a>
</p>
</div>
<h3>编辑</h3>
<hr/>
<!--留言提交-->
<form action="edit.php" method="post">
<fieldset>
<label>用户名</label><input type="text" name="username" class="username" value="<?php echo $editmsgs['username'];?>" required>
<label>标题</label><input type="text" name="title" class="title" value="<?php echo $editmsgs['title'];?>" required>
<label>内容</label><textarea name="content" rows="5" cols="30" class="content" value="<?php echo $editmsgs['content'];?>" required></textarea><br/>
<input type="submit" class="btn btn-primary btn-lg" name="editMsg" value="编辑完成">
<button><div class="check"><a href="index.php">查看留言</a></div></button>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
edit.php 无die;显示编辑页面但依然只能编辑第二条数据
<?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);//反序列化字符串
}
}
//获取数组的键名
$editkey=$_GET['id'];
$editmsgs=$msgs[$editkey];
var_dump($editmsgs);
//判断是否点击编辑按钮
if (isset($_POST['editMsg'])) {
$editmsgs['username']=$_POST['username'];
$editmsgs['title']=$_POST['title'];
$editmsgs['content']=$_POST['content'];
$editmsgs['time']=time();
//将指定位置的数据修改为接收到的新数据。
$msgs[$editkey]=$editmsgs;
//将所有数据序列化
$msgs=serialize($msgs);
//向文件中重写内容
if (file_put_contents($filename, $msgs)) {
echo "<script>alert('编辑成功!');location.href='index.php';</script>";
}else{
echo "<script>alert('编辑失败!');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>慕课留言板</title>
<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>
<style type="text/css">
form{
text-align: center;
}
button{
width: 82px;
height: 30px;
}
.check a:link{
color: #0C0C0C;
text-decoration: none;
}
.check a:visited{
color: #0C0C0C;
text-decoration: none;
}
.username{
width: 440px;
}
.title{
width: 440px;
}
.content{
width: 440px;
height: 220px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="page-header">
<h1>
慕课留言板-<span>V1.0</span>
</h1>
</div>
<div class="hero-unit">
<h1>
来都来了,就说点什么吧~~~~
</h1>
<p>
把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。
</p>
<p>
<a rel="nofollow" class="btn btn-primary btn-large" href="#">查看更多 ?</a>
</p>
</div>
<h3>编辑</h3>
<hr/>
<!--留言提交-->
<form action="edit.php" method="post">
<fieldset>
<label>用户名</label><input type="text" name="username" class="username" value="<?php echo $editmsgs['username'];?>" required>
<label>标题</label><input type="text" name="title" class="title" value="<?php echo $editmsgs['title'];?>" required>
<label>内容</label><textarea name="content" rows="5" cols="30" class="content" value="<?php echo $editmsgs['content'];?>" required></textarea><br/>
<input type="submit" class="btn btn-primary btn-lg" name="editMsg" value="编辑完成">
<button><div class="check"><a href="index.php">查看留言</a></div></button>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
好帮手慕小尤
2019-08-18 11:24:48
同学你好,在index.php文件中为edit.php文件传递的参数名为id而不是editkey。代码如下:
<?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);//反序列化字符串
}
}
// 获取数组的键名
$editkey=$_GET['id'];
$editmsgs=$msgs[$editkey];
var_dump($editmsgs);die;祝学习愉快!
PHP小白零基础入门
- 参与学习 人
- 提交作业 626 份
- 解答问题 4928 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星