编辑之后不显示键值,变成了添加数据

编辑之后不显示键值,变成了添加数据

<?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>

首页

<?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回答

你好,逻辑没有问题,编辑也是没有问题的,你注意一下你的编辑页面的$_GET['editkey'],打印一下看看,如果这个值为空,你的编辑就会变成新增,并且新增的数据的下标是一个空字符串"",在编辑页面你其实使用了一个input框来存放要编辑的留言id,而$_GET['editkey']是最初从首页点击编辑时跳转到编辑页面接收的id,当你点击编辑完成时页面会提交数据,因为提交到的是本页面,所以本页面会再刷新一次,此时你可以使用post提交的edit值,如下:

http://img1.sycdn.imooc.com//climg/5ab4a82000018d4113790168.jpg

http://img1.sycdn.imooc.com//climg/5ab4a80a0001130b14470638.jpg

修改如上如果还是有问题,那就是你的首页点击编辑按钮时传过来的editkey就有问题,这应该跟你的add添加逻辑有关,你的首页编辑按钮链接是没错的,所以查看一下你在首页点击编辑按钮时是什么情况

查看方式:将鼠标放到编辑按钮上看右下角出现的链接,看下面这种情况,editkey=后面什么都没有,后面编辑会出错

http://img1.sycdn.imooc.com//climg/5ab4a9590001779418280501.jpg

如果解决了你的问题,请采纳,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师