编辑页面出现的问题

编辑页面出现的问题

/Applications/MAMP/htdocs/phpstorm/edit.php:37:string 'a:1:{s:6:' (length=9)

这是我特地对$eidtkey var_dump出来的结果;

请问这个是什么原因造成的,该如何解决?目前只有第一条留言的$key可以获取,但是无法编辑,可以删除!

edit.php

<?php
date_default_timezone_set('PRC');
header('content-type:text/html;charset=utf-8');
$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['sub'])){
    $editkey=$_POST['edit'];
    if($editkey==""){
        echo "<script>alert('编辑失败,请重新编辑!');location.href='35-index.php';</script>";
    }
    $username=$_POST['username'];
    $title=$_POST['title'];
    $content=$_POST['content'];
    $time=time();
    $data=compact('username','title','content','time');
    $msgs[$editkey] = $data;
    $msgs=serialize($msgs);
    if(file_put_contents($filename,$msgs)){
        echo "<script>alert('编辑成功!');location.href='35-index.php';</script>";
    }else{
        echo "<script>alert('编辑失败!');location.href='35-index.php';</script>";
    }
    // array_push($msgs,$data);
    // $msgs=serialize($msgs);
    // var_dump($msgs);
}
var_dump($editkey);
?>
<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><span>欢迎您!</span>Jokky</small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    Hello, world!
                </h1>
                <p>
                    这是一个简易的留言板!
                </p>
            </div>
            <hr/>
            <form action="35-index.php" method="post">
                <fieldset>
                    <legend>留言板</legend>
                    <label>用户名:</label><input type="text" name="username" placeholder="请输入姓名" value="<?php echo $msgs[$editkey]['username'];?>" required/>
                    <label>标题:</label><input type="text" name="title" placeholder="请输入标题" value="<?php echo $msgs[$editkey]['title'];?>" required/>
                    <label>内容:</label><textarea name="content" rows="5" cols="30" required><?php echo $msgs[$editkey]['content'];?></textarea>
                    <hr/>
                    <input type="submit" name="sub" value="编辑完成" class="btn btn-primary"/>
                    <a href="35-index.php" class="btn btn-default">查看留言</a>
                    <input type="hidden" name="edit" value="<?php echo $editkey;?>"/>
                </fieldset>
            </form>
        </div>
    </div>
</div>
</body>
</html>

35-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['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);
// }
?>
<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><span>欢迎您!</span>Jokky</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>
                    <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("Y/m/d H:i:s",$val['time']);?>
                        </td>
                        <td>
                            <?php echo $val['content']?>
                        </td>
                        <td>
                            <a href="edit.php?editkey=<?php echo $key?>" class="btn btn-danger" >编辑</a>
                        </td>
                        <td>
                            <a href="delect.php?delect=<?php echo $key?>" class="btn btn-warning">删除</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
<?php
endif;
?>
            <hr/>
            <a href="add.php" class="btn btn-primary">我要留言</a>
        </div>
    </div>
</div>
</body>
</html>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

你好,小慕试了一下你的代码,点击编辑获取key没有问题,打印出来的eidtkey是正常的,检查一下你的msg.txt,可能是原来错乱数据导致的,都删了,重新添加数据,再执行编辑删除操作看看,如果还是有问题,那很可能你的数据添加有问题,另外,你的编辑逻辑写在了edit.php,不是35-index.php,编辑后提交到本页面即可。

以下是你edit.php的编辑实现:

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

应提交至本页面

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

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


  • Jokky 提问者 #1
    后来发现了,是首页<a href>中引号要是单引号的问题
    2018-06-29 13:26:53
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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