为什么编号为1的数据删不掉?别的都能删
这是主页面的代码
<?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']);//从字符串中去除 HTML 和 PHP 标记 $content=strip_tags($_POST['content']); $time=time(); //将其组成关联数组 $data=compact('username','title','content','time'); //判断是编辑留言还是添加留言 if(isset($_POST['editkey'])){ $key=$_POST['editkey']; $msgs[$key]=$data; } else{ array_push($msgs,$data);} $msgs=serialize($msgs); if(file_put_contents($filename,$msgs)) { //将一个字符串写入文件 if (isset($_POST['editkey'])) { echo "<script>alert('编辑成功!');location.href='index.php';</script>"; } else { echo "<script>alert('留言成功!');location.href='index.php';</script>"; } } else{ if(isset($_POST['editkey'])) { echo "<script>alert('编辑失败,请重试!');location.href='index.php';</script>"; }else{ echo "<script>alert('留言失败,请重试!');location.href='index.php';</script>"; } } } /* $msgs=[ [...], [...] ]; file_get_contents($filename);得到文件中的内容,返回的是字符串 file_put_contents($filename,$data);向指定文件写内容,如果文件不存在,会创建 serialize($str);序列化字符串 unserialize($str);反序列化 */ //print_r($msgs); ?> <!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> 留言板-<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 $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("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>
这是删除的代码
<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC'); $filename='msg.txt'; $msgs=array(); if(isset($_GET['delkey'])&&!empty($_GET['delkey'])){ $delkey=$_GET['delkey']; } if(file_exists($filename)) { $string = file_get_contents($filename);//获取文本内容 if (strlen($string) > 0) { $msgs = unserialize($string);//反序列化赋值 } } else{ echo '文件不存在'; } if(!isset($msgs[$delkey])){ echo "<script>alert('数据出错,请重试!');location.href='index.php';</script>"; } unset($msgs[$delkey]);//销毁指定的变量 $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>"; } ?
107
收起
正在回答
4回答
你好,老师删除时没有尝试删除第一条操作,没有发现错误抱歉,正确的代码如下:
首页修改部分为:
<?php $i=1;foreach($msgs as $key=>$val):?> <tr class="success"> <td> <?php echo $key++;?>//修改留言id值的显示 </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;?>
删除页面修改部分为:
if(isset($_GET['delkey'])&&!empty($_GET['delkey'])){ $delkey=$_GET['delkey']-1;//根据id值减一 }
如果解决了您的问题请采纳,祝学习愉快!
发呆发呆
2017-08-22 17:26:31
添加留言的代码
<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC'); ?> <!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> 留言板-<small>V1.0</small> </h1> </div> <div class="hero-unit"> <h1> Hello </h1> <p> 来了就说二句吧~~~ </p> </div> <div style="width: 100%;text-align:center"> <form class="form-horizontal center-block" action="index.php" method="post"> <fieldset> <legend>请留言</legend> <label>用户名</label><input style="" type="text" name="username" required/> <label>标题</label><input style="" type="text" name="title" required/> <label>内容</label><textarea name="content" rows="10" cols="30"></textarea> <hr/> <input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="发布留言" /> <input type="submit" class="btn btn-default btn-lg" value="查看留言" onclick="window.location.href='index.php'"/> </fieldset> </form> </div> </div> </div> </div> </body> </html>
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星