老师,编辑留言,点击提交后,实现不了编辑功能,而是在主页新增了留言,请老师帮忙
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$editkey=$_GET['editkey'];
$filename = "msg.txt";
$msgs=array();
if (file_exists($filename)) {
$string = file_get_contents($filename);
if (strlen($string)>0) {
$msgs = unserialize($string);
}
$editarr = $msgs[$editkey];
if (!is_array($editarr)||count($editarr)<=0) {
echo "<script>alert('留言出错,无法编辑!');location.href='index.php'</script>";
}
}
?>
<html>
<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>
Seven留言板-<small>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</h1>
<p>
此处可以更新您的留言后,然后重新发布哦!
</p>
</div>
<form action="index.php" method="post" >
<fieldset>
<legend>编辑</legend>
<label>用户名</label>
<input type="text" name="username" required value="<?php echo $editarr['username'] ?>"/>
<label>标题</label>
<input type="text" name="title" required value="<?php echo $editarr['title'] ?>"/>
<label>用户名</label>
<textarea name="content" rows="10" cols="30" required ><?php echo $editarr['content'] ?></textarea>
<?php echo '<br/>'; ?>
<button type="submit" class="btn btn-primary" name="pubMsg" >编辑完成</button>
<?php echo "|";?>
<a href="index.php"><button type="button" contenteditable="true">查看留言</button></a>
</fieldset>
<input type="hidden" name="editkey" value="<?php echo $editkey ?>"/>
</form>正在回答
同学,您好!编辑功能因为涉及到不止一个页面,您就贴了一个页面的代码,小慕也没法确定具体是那个地方出错了。这样,小慕重新给您梳理下实现整个编辑功能的思路,希望您自己能够缕缕,实在还是搞不定的话,就把涉及到编辑功能的页面所有代码都贴出来
编辑功能整体思路如下:
(1)在“留言展示”页面操作下面会有一个“编辑“链接(这个链接会带着一个唯一标识),点击这个“编辑”就会跳到一个编辑的页面,具体可参考截图:

(2)在编辑页面中接受展示页传过来的唯一标识,根据唯一标识取出对应的这条数据在编辑页面做回写展示这条数据,编辑页面的表单还要能够让用户实现编辑,表单中会带着一个隐藏域把唯一标识继续传到编辑页面表单提交的页面(这里我们假设把表单提交到留言展示页面)。


(3)在留言展示页面POST方式接受隐藏域的唯一标识以及接受编辑页面用户重新编辑的数据,然后把接受到的数据序列化以后存到文件中去,存文件中去了要判断一下,如果有文件中有传过来的唯一标识的就弹出“编辑成功”的提示框否则就弹出“留言成功”的提示框哦。

如果解决了您的问题,请采纳!祝学习愉快!
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename = "msg.txt";
$msgs = [];
//检测文件
if(($filename)){
//读取文件
$string = file_get_contents($filename);
if (strlen($string)>0) {
$msgs = unserialize($string);
}
}
//检测提交
if (isset($_POST['pubMsg'])) {
$username = $_POST['username'];
$title = strip_tags($_POST['title']);
$content = strip_tags($_POST['content']);
$time = time();
//数组化
$data = compact('username','title','content','time');
array_push($msgs,$data);
$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>
<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>
Seven留言板-<small>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</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>
<tbody>
<?php $i=1; foreach ($msgs as $key=>$val): ?>
<tr class="success">
<td>
<?php echo $key+1; ?>
</td>
<td>
<?php echo $val['username']; ?>
</td>
<td>
<?php echo $val['title']; ?>
</td>
<td>
<?php echo date('Y-m-d H:i:s',$val['time']); ?>
</td>
<td>
<?php echo $val['content']; ?>
</td>
<td>
<a href="javascript:;" onclick="del(<?php echo $key; ?>);return false;">删除</a>
<?php echo "|"; ?>
<a href="edit.php?editkey=<?php echo $key ?>">编辑</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<input type="submit" class="btn btn-primary btn-lg" onclick="window.location.href='add.php'" name="zt" value="发布留言">
</div>
</div>
</div>
</body>
</html>这个是主页的代码,然后直接到编辑页面的已经贴出去了,老师您看看。
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$editkey=$_GET['editkey'];
$filename = "msg.txt";
if (isset($_GET['editkey'])) {
$editkey = $_GET['editkey'];
}
$filename = "msg.txt";
$msgs=array();
if (file_exists($filename)) {
$string = file_get_contents($filename);
if (strlen($string) > 0) {
$msgs = unserialize($string);
}
}
$editarr = $msgs[$editkey];
if (isset($_POST['pubMsg'])) {
$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();
$msgs=serialize($msgs);
if (file_put_contents($filename,$msgs)) {
echo "<script>alert('修改成功!');location.herf='./index.php';</script>";
}else {
echo "<script>alert('修改失败!');location.herf='./index.php';</script>";
}
}
if(!is_array($editarr)||count($editarr)<=0){
echo "<script>alert('留言出错,无法编辑!');location.herf='./index.php';</script>";
}
?>
<html>
<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>
Seven留言板-<small>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</h1>
<p>
此处可以更新您的留言后,然后重新发布哦!
</p>
</div>
<form action="index.php" method="post" >
<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>
<?php echo '<br/>'; ?>
<button type="submit" class="btn btn-primary" name="pubMsg" >编辑完成</button>
<?php echo "|";?>
<a href="index.php"><button type="button" contenteditable="true">查看留言</button></a>
</form>奔溃了,完全按照老师发的代码还是失败,点击编辑留言后增加留言,实现不了编辑。
你好,首先点击编辑完成时要提交编辑后的和内容还必须传递留言的唯一标识id,
编辑页面根据id确定是编辑的那条留言,并显示在编辑页面,
如:
<?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);
}
}
$id=$_GET['id'];//检测用户编辑的是那条的留言id
if(!empty($id)){
$id=$_GET['id']-1;//根据自己页面显示的情况,如果页面显示是下表没有加“1”的情况下不需要再减“1”
echo "$id";
}
//对数组进行重新排序
$msgs=array_merge($msgs);然后根据id显示编辑页面的留言:
<label>用户名</label><input type="text" name="username" required value="<?php echo $msgs[$id]["username"]?>" /> <label>标题</label><input type="text" name="title" required value="<?php echo $msgs[$id]["title"]?>" /> <label>内容</label><textarea name="content" rows="5" cols="30" required ><?php echo $msgs[$id]["content"]?> </textarea>
然后再通过form表单提交:
if(isset($_POST['pubMsg'])){
$msgs[$id]["username"]=$_POST['username'];
$msgs[$id]["title"]=strip_tags($_POST['title']);
$msgs[$id]["content"]=strip_tags($_POST['content']);
$msgs[$id]["time"]=time();
//将其组成关联数组
$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积分~
来为老师/同学的回答评分吧
0 星