能获取数据,但是提交修改后只会刷新deit页面,而且还跳出了错误报告,原因是?
index
<?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['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 lang="en">
<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>
IMOOC留言板-<small>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
慕课留言板,欢迎你!
</h1>
<p>
<em>慕课网是垂直的互联网IT技能</em>免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好<em>的互联网</em>技术牛人,也可以通过免费的在线公开视频课程学习国内领先<em>的互联网IT技</em>术。
</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 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("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="javascript:;" onclick="del(<?php echo $key ?>);return false;">删除</a>
<script type="text/javascript">
function del(key){
if(confirm("确定删除吗")){
location.href="delete.php?delkey="+key;
}else{
location.href="index.php";
}
}
</script>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<a class="btn btn-primary btn-large" href="add.php">我要留言</a>
</div>
</div>
</div>
<hr>
<div class="footer">
慕课商城© 2017 powered by <strong>IMooc.inc</strong>
</div>
</body>
</html>
edit
<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$getZ=$_GET['editkey'];
$filename="msg.txt";
$string=file_get_contents($filename);
$msgs=unserialize($string);
$username1=$msgs[$getZ]['username'];
$title1=$msgs[$getZ]['title'];
$content1=$msgs[$getZ]['content'];
if(isset($pubMsg)){
$username=$_POST['username'];
$title=$_POST['title'];
$content=$_POST['content'];
$time=time();
$date=compact('username','title','content','time');
if(file_put_contents($filename,serialize($msgs))){
echo "<script>alert('修改成功');location.href='index.php'</script>";
}else{
echo "<script>alert('修改失败');location.href='index.php'</script>";
}
}
?>
<!doctype html>
<html lang="en">
<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>
IMOOC留言板-<small>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
既然来了,就说点什么吧~~~~
</h1>
<p>
把你想说的写在下边,标注好你的姓名,主题,点击提交发送给我们,让小伙伴们都知道你在想些什么
</p>
</div>
<form action="edit.php" method="post">
<legend>发布</legend>
<label>用户名</label><input type="text" name="username" value="<?php echo $username1; ?>" required/>
<label>标题</label><input type="text" name="title" value="<?php echo $title1; ?>" required/>
<label>内容</label><textarea name="content" id="" rows="5" cols="30" required><?php echo $content1; ?></textarea><br>
<input type="hidden" name="getZ" id="getZ" value="<?php echo $getZ; ?>">
<input type="submit" value="编辑完成" name="pubMsg" class="btn btn-primary btn-lg"/>
<a class="btn btn-lg" href="index.php">查看留言</a>
</form>
</div>
</div>
</div>
</body>
</html>
正在回答
同学你好,1. $pubMsg变量是不存在的,建议使用post进行判断。
2. 接收唯一标识,并将接收到数据,赋值到相应的数组中。修改后代码如下:
<?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); } } $getZ=$_GET['editkey']; $username1=$msgs[$getZ]['username']; $title1=$msgs[$getZ]['title']; $content1=$msgs[$getZ]['content']; // 判断是否是post传值,如果是则进行修改 if($_POST){ $username=$_POST['username']; $title=$_POST['title']; $content=$_POST['content']; $time=time(); $date=compact('username','title','content','time'); // 接收HTML传递的唯一标识 $key = $_POST['getZ']; // 将接收到数据,赋值到相应的数组下键中 $msgs[$key] = $date; if(file_put_contents($filename,serialize($msgs))){ echo "<script>alert('修改成功');location.href='index.php'</script>"; }else{ echo "<script>alert('修改失败');location.href='index.php'</script>"; } } ?>
祝学习愉快!
- 参与学习 人
- 提交作业 626 份
- 解答问题 4930 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星