为什么我编辑之后没有效果

为什么我编辑之后没有效果

index文件

<?php  // 思路 创建保存文件   检测文件是否存在 读取文件内容  判断文件有没有内容  因为文件里是字符串所以要反序列化文件得到数组 遍历给表格里
//// 点了按钮就是把内容写到文件里 给留言跟按钮添加post 检测用户是否点击了提交按钮  post赋值给变量   变量组成关联数组 写到文件里
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['pug'])){
    $username=$_POST['username'];
    $title=strip_tags($_POST['title']); //从字符串中去除 HTML 和 PHP 标记 防止攻击strip_tags
    $content=strip_tags($_POST['content']);
    $time=time();
    //将其组成关联数组
    $data= compact('username','title','content','time');
    //array_push将一个或多个单元压入数组的末尾(入栈)
    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>";
    }
}



/*   保存留言的方式 二维的数组  webpage.php
 $msgs=[
[..], 第一条留言
[..],  第二条留言
];
file_get_contents($filename):  得到文件中的内容 ,返回的是字符串
file_put_contents($filename):  向指定文件写内容  只能是字符串  如果文件不存在会创建
serialize($str):  对字符串 序列化 序列化之后就能写入文件
unserialize($str): 反序列化  读取文件  是数组
*/

?>
<!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" id="LG">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    <small><span>IMOOC留言板-v1.0</span></small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    既然来了,就说点什么吧~~~~~
                </h1>
                <p>

                </p>

            </div>
            <!-- 检查是不是数组并且 计算数组中的单元数目大于0 就显示这个表格          -->
            <?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 $val):?>
                        <tr class="success" >
                            <td >
                                <?php  echo$a=$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>
                               <!--    get要用?号链接                             -->
                               <a href="deit.php?id=<?php echo $a ?>" >编辑</a> | <a>删除</a>
                            </td>
                        </tr>

                    <?php  endforeach;?>
                    </tbody>
                </table>

            <?php endif;?>
            <form action="add.php" method="post">
                <fieldset>
                    <input type="submit" class="btn btn-primary btn-lg"  value="我要留言" name="pubMsg">
                </fieldset>
            </form>
        </div>
    </div>
</div>
</body>
</html>

deit文件

<?php
date_default_timezone_set('PRC');
$num= $_GET['id']-1; // 键名
$filename="msg.txt"; //文件
$msgs=[];
if (file_exists($filename)){
     $string=file_get_contents($filename);//读取文件内容 字符串
     $msgs=unserialize($string); //反序列化 得到数组
}

$name= $msgs[$num]['username']; //username 用户名
$title=$msgs[$num]['title']; //title   标题
$content=$msgs[$num]['content'];// 内容

//检测用户是否点击了编辑完成按钮
if (isset($_POST['pug_set'])){
    $name=$_POST['username'];
    $title=strip_tags($_POST['title']);
    $content=strip_tags($_POST['content']);
    $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>";
    }


}




?>


<!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 onload="init_1()">
<div class="container-fluid" id="LG">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    <small><span>IMOOC留言板-v1.0</span></small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    既然来了,就说点什么吧~~~~~
                </h1>
                <p>

                </p>

            </div>

            <form action="index.php" method="post">
                <fieldset>
                    <legend>发布</legend>
                    <label>用户名</label><input type="text" name="username"value="<?php echo $name ;?>"  required />

                    <label>标题</label><input type="text" name="title"value="<?php echo $title ;?>" required />
                    <label>内容</label><textarea name="content" rows="5" cols="30"   required ><?php echo $content;   ?></textarea><hr>
                    <input type="submit" class="btn btn-primary btn-lg"  value="编辑完成" name="pug_set">
                    <input  type="button" class="btn btn-default btn-lg"  value="查看留言" name="look" id="look" onclick="init_1()">
                    <script > function init_1() {
                            document.getElementById("look").onclick=function () {
                                window.location.href="index.php"
                            }
                        }</script>
                </fieldset>
            </form>


正在回答

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

2回答

同学你好,因同学在编辑页面跳转的index.php文件,所以未修改成功。并调整了一下同学的代码,修改后代码如下:

index.php:

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

edit.php

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

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

祝学习愉快!

  • 昵称加载中__ 提问者 #1
    老师照你的改了 我编辑留言之后报错 Not Found The requested URL /liuyanban/edit.php was not found on this server.
    2019-10-26 17:34:06
  • 同学你好,同学的文件名为deit.php ,请同学讲index.php文件中的跳转路径修改为自己的路径,如: <a href="deit.php?id=<?php echo $key ?>" >编辑</a>。祝学习愉快!
    2019-10-26 17:37:12
  • <a href="deit.php?id=<?php echo $key ?>" >编辑</a> 我是这个路径啊 我跳转到编辑页面 然后改了文字 点编辑 就报错了 Not Found The requested URL /liuyanban/edit.php was not found on this server.
    2019-10-26 17:41:22
提问者 昵称加载中__ 2019-10-26 17:44:06
<?php  // 思路 创建保存文件   检测文件是否存在 读取文件内容  判断文件有没有内容  因为文件里是字符串所以要反序列化文件得到数组 遍历给表格里
//// 点了按钮就是把内容写到文件里 给留言跟按钮添加post 检测用户是否点击了提交按钮  post赋值给变量   变量组成关联数组 写到文件里
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['pug'])){
    $username=$_POST['username'];
    $title=strip_tags($_POST['title']); //从字符串中去除 HTML 和 PHP 标记 防止攻击strip_tags
    $content=strip_tags($_POST['content']);
    $time=time();
    //将其组成关联数组
    $data= compact('username','title','content','time');
    //array_push将一个或多个单元压入数组的末尾(入栈)
    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>";
    }
}

//检测用户是否点击删除按钮
if (isset($_GET['id']))

/*   保存留言的方式 二维的数组  webpage.php
 $msgs=[
[..], 第一条留言
[..],  第二条留言
];
file_get_contents($filename):  得到文件中的内容 ,返回的是字符串
file_put_contents($filename):  向指定文件写内容  只能是字符串  如果文件不存在会创建
serialize($str):  对字符串 序列化 序列化之后就能写入文件
unserialize($str): 反序列化  读取文件  是数组
*/

?>
<!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" id="LG">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    <small><span>IMOOC留言板-v1.0</span></small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    既然来了,就说点什么吧~~~~~
                </h1>
                <p>

                </p>

            </div>
            <!-- 检查是不是数组并且 计算数组中的单元数目大于0 就显示这个表格          -->
            <?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$a=$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>
                               <!--    get要用?号链接                             -->
                               <a href="deit.php?id=<?php echo $key ?>" >编辑</a> |
                                <a href="delete.php?id=<?php echo $key ?>">删除</a>
                            </td>
                        </tr>

                    <?php  endforeach;?>
                    </tbody>
                </table>

            <?php endif;?>
            <form action="add.php" method="post">
                <fieldset>
                    <input type="submit" class="btn btn-primary btn-lg"  value="我要留言" name="pubMsg">
                </fieldset>
            </form>
        </div>
    </div>
</div>
</body>
</html>
<?php
date_default_timezone_set('PRC');
$num= $_GET['id']; // 键名
var_dump($num);
$filename="msg.txt"; //文件
$msgs=[];

     $string=file_get_contents($filename);//读取文件内容 字符串
     $msgs=unserialize($string); //反序列化 得到数组



$username= $msgs[$num]['username']; //username 用户名
$title=$msgs[$num]['title']; //title   标题
$content=$msgs[$num]['content'];// 内容

//检测用户是否点击了编辑完成按钮
if (isset($_POST['pug_set'])){
    $username=$_POST['username'];
    $title=strip_tags($_POST['title']);
    $content=strip_tags($_POST['content']);
    $time=time();
    //组成关联数组
    $data=compact('username','title','content','time');
//接收隐藏域
    $key=$_POST['id'];
    $msgs[$key]=$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 onload="init_1()">
<div class="container-fluid" id="LG">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    <small><span>IMOOC留言板-v1.0</span></small>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    既然来了,就说点什么吧~~~~~
                </h1>
                <p>

                </p>

            </div>

            <form action="edit.php" method="post">
                <fieldset>
                    <legend>发布</legend>
                    <label>用户名</label><input type="text" name="username"value="<?php echo $username ;?>"  required />

                    <label>标题</label><input type="text" name="title"value="<?php echo $title ;?>" required />
                    <label>内容</label><textarea name="content" rows="5" cols="30"   required ><?php echo $content;   ?></textarea><hr>
                    <input type="hidden" name="id" value="<?php echo $num ?>">
                    <input type="submit" class="btn btn-primary btn-lg"  value="编辑完成" name="pug_set">
                    <input  type="button" class="btn btn-default btn-lg"  value="查看留言" name="look" id="look" onclick="init_1()">
                    <script > function init_1() {
                            document.getElementById("look").onclick=function () {
                                window.location.href="index.php"
                            }
                        }</script>
                </fieldset>
            </form>

http://img1.sycdn.imooc.com//climg/5db4154d0935acd719200937.jpg点了这个

就变成了这样http://img1.sycdn.imooc.com//climg/5db415640945d97b06480123.jpg

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

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

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

0 星
PHP小白零基础入门
  • 参与学习           人
  • 提交作业       626    份
  • 解答问题       4930    个

想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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