编辑功能只能编辑第二条数据,麻烦老师帮忙看一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 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 [ 'leaveMsg' ])){ $username = $_POST [ 'username' ]; $title = strip_tags ( $_POST [ 'title' ]); //过滤留言信息中的HTML和PHP标签 $content = strip_tags ( $_POST [ 'content' ]); $time = time(); //将其组成关联数组 $data = compact( 'username' , 'title' , 'content' , 'time' ); array_push ( $msgs , $data ); $msgs = serialize( $msgs ); //序列化字符串 } if (isset( $_GET [ 'delkey' ])){ $key = $_GET [ 'delkey' ]; //要删除的数据不存在 if (!isset( $msgs [ $key ])){ echo "<script>alert('数据出错,请重试!');location.href='index.php';</script>" ; } unset( $msgs [ $key ]); $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> <title>慕课留言板</title> <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> <script type= "text/javascript" > function del(key){ if (confirm( "确定删除?" )){ location.href= "delete.php?delkey=" +key; } else { location.href= "index.php" ; } } </script> </head> <body> <div class = "container-fluid" > <div class = "row-fluid" > <div class = "span12" > <div class = "page-header" > <h1> 慕课留言板-<span>V1.0</span> </h1> </div> <div class = "hero-unit" > <h1> Hello, World! </h1> <p> 一个自由,平等与民主的留言板, 你可以自由友善的发言。 </p> <p> <a rel= "nofollow" class = "btn btn-primary btn-large" href= "#" >查看更多 »</a> </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 $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 ( "m/d/Y H:i:s" , $val [ 'time' ]);?> </td> <td> <?php echo $val [ 'content' ];?> </td> <td> <a href= "edit.php?id=<?php echo $key ?>" >编辑</a> | <a href= "javascript:;" onclick= "del(<?php echo $key?>);return false;" >删除</a> </td> </tr> <?php } ?> </tbody> </table> <?php ; ?> <form action= "#" method= "post" > <fieldset> <input type= "submit" class = "btn leave message btn-lg" name= "leaveMsg" value= "我要留言" > </fieldset> </form> </div> </div> </div> <hr> <div class = "footer" > <p><span>慕课商城</span>©2017 powered by IMooc.inc</p> </div> <!--跳转留言页面--> <?php if (isset( $_POST [ 'leaveMsg' ])) { $leaveMsg = $_POST [ 'leaveMsg' ]; header( "Location: add.php" ); } else { exit ; } ?> </body> </html> edit.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 ); //反序列化字符串 } } // 获取数组的键名 $editkey = $_GET [ 'editkey' ]; $editmsgs = $msgs [ $editkey ]; //判断是否点击编辑按钮 if (isset( $_POST [ 'editMsg' ])) { $editmsgs [ 'username' ]= $_POST [ 'username' ]; $editmsgs [ 'title' ]= $_POST [ 'title' ]; $editmsgs [ 'content' ]= $_POST [ 'content' ]; $editmsgs [ 'time' ]=time(); //将指定位置的数据修改为接收到的新数据。 $msgs [ $editkey ]= $editmsgs ; //将所有数据序列化 $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> <title>慕课留言板</title> <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> <style type= "text/css" > form{ text-align: center; } button{ width: 82px; height: 30px; } .check a:link{ color: #0C0C0C; text-decoration: none; } .check a:visited{ color: #0C0C0C; text-decoration: none; } .username{ width: 440px; } .title{ width: 440px; } .content{ width: 440px; height: 220px; } </style> </head> <body> <div class = "container-fluid" > <div class = "row-fluid" > <div class = "span12" > <div class = "page-header" > <h1> 慕课留言板-<span>V1.0</span> </h1> </div> <div class = "hero-unit" > <h1> 来都来了,就说点什么吧~~~~ </h1> <p> 把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。 </p> <p> <a rel= "nofollow" class = "btn btn-primary btn-large" href= "#" >查看更多 ?</a> </p> </div> <h3>编辑</h3> <hr/> <!--留言提交--> <form action= "edit.php" method= "post" > <fieldset> <label>用户名</label><input type= "text" name= "username" class = "username" value= "<?php echo $editmsgs['username'];?>" required> <label>标题</label><input type= "text" name= "title" class = "title" value= "<?php echo $editmsgs['title'];?>" required> <label>内容</label><textarea name= "content" rows= "5" cols= "30" class = "content" value= "<?php echo $editmsgs['content'];?>" required></textarea><br/> <input type= "submit" class = "btn btn-primary btn-lg" name= "editMsg" value= "编辑完成" > <button><div class = "check" ><a href= "index.php" >查看留言</a></div></button> </fieldset> </form> </div> </div> </div> </body> </html> |
点击第四条数据后出现第二条数据的标题和用户名,没有内容。编辑内容后跳转index页后发现第二条数据被修改,添加数据和删除数据功能都没问题
3
收起
正在回答
4回答
同学你好,在edit文件中添加input标签,如下:
1 | <input type= "hidden" name= "id" value= "<?php echo $editkey ?>" > |
与form表单一起提交。然后在PHP代码中进接收。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //判断是否点击编辑按钮 if (isset( $_POST [ 'editMsg' ])) { $data [ 'username' ]= $_POST [ 'username' ]; $data [ 'title' ]= $_POST [ 'title' ]; $data [ 'content' ]= $_POST [ 'content' ]; $data [ 'time' ]=time(); //将指定位置的数据修改为接收到的新数据。 $id = $_POST [ 'id' ]; $msgs [ $id ]= $data ; // var_dump($id);die; //将所有数据序列化 $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>" ; } } |
祝学习愉快!
断线纸鸢1
2019-08-18 12:03:47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | edit.php 有 die ;只显示数组 <?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 ); //反序列化字符串 } } //获取数组的键名 $editkey = $_GET [ 'id' ]; $editmsgs = $msgs [ $editkey ]; var_dump( $editmsgs ); die ; //判断是否点击编辑按钮 if (isset( $_POST [ 'editMsg' ])) { $editmsgs [ 'username' ]= $_POST [ 'username' ]; $editmsgs [ 'title' ]= $_POST [ 'title' ]; $editmsgs [ 'content' ]= $_POST [ 'content' ]; $editmsgs [ 'time' ]=time(); //将指定位置的数据修改为接收到的新数据。 $msgs [ $editkey ]= $editmsgs ; //将所有数据序列化 $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> <title>慕课留言板</title> <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> <style type= "text/css" > form{ text-align: center; } button{ width: 82px; height: 30px; } .check a:link{ color: #0C0C0C; text-decoration: none; } .check a:visited{ color: #0C0C0C; text-decoration: none; } .username{ width: 440px; } .title{ width: 440px; } .content{ width: 440px; height: 220px; } </style> </head> <body> <div class = "container-fluid" > <div class = "row-fluid" > <div class = "span12" > <div class = "page-header" > <h1> 慕课留言板-<span>V1.0</span> </h1> </div> <div class = "hero-unit" > <h1> 来都来了,就说点什么吧~~~~ </h1> <p> 把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。 </p> <p> <a rel= "nofollow" class = "btn btn-primary btn-large" href= "#" >查看更多 ?</a> </p> </div> <h3>编辑</h3> <hr/> <!--留言提交--> <form action= "edit.php" method= "post" > <fieldset> <label>用户名</label><input type= "text" name= "username" class = "username" value= "<?php echo $editmsgs['username'];?>" required> <label>标题</label><input type= "text" name= "title" class = "title" value= "<?php echo $editmsgs['title'];?>" required> <label>内容</label><textarea name= "content" rows= "5" cols= "30" class = "content" value= "<?php echo $editmsgs['content'];?>" required></textarea><br/> <input type= "submit" class = "btn btn-primary btn-lg" name= "editMsg" value= "编辑完成" > <button><div class = "check" ><a href= "index.php" >查看留言</a></div></button> </fieldset> </form> </div> </div> </div> </body> </html> edit.php 无 die ;显示编辑页面但依然只能编辑第二条数据 <?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 ); //反序列化字符串 } } //获取数组的键名 $editkey = $_GET [ 'id' ]; $editmsgs = $msgs [ $editkey ]; var_dump( $editmsgs ); //判断是否点击编辑按钮 if (isset( $_POST [ 'editMsg' ])) { $editmsgs [ 'username' ]= $_POST [ 'username' ]; $editmsgs [ 'title' ]= $_POST [ 'title' ]; $editmsgs [ 'content' ]= $_POST [ 'content' ]; $editmsgs [ 'time' ]=time(); //将指定位置的数据修改为接收到的新数据。 $msgs [ $editkey ]= $editmsgs ; //将所有数据序列化 $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> <title>慕课留言板</title> <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> <style type= "text/css" > form{ text-align: center; } button{ width: 82px; height: 30px; } .check a:link{ color: #0C0C0C; text-decoration: none; } .check a:visited{ color: #0C0C0C; text-decoration: none; } .username{ width: 440px; } .title{ width: 440px; } .content{ width: 440px; height: 220px; } </style> </head> <body> <div class = "container-fluid" > <div class = "row-fluid" > <div class = "span12" > <div class = "page-header" > <h1> 慕课留言板-<span>V1.0</span> </h1> </div> <div class = "hero-unit" > <h1> 来都来了,就说点什么吧~~~~ </h1> <p> 把你想说的都写在下面,点击发布留言,和小伙伴们交流,请友善发言哦。 </p> <p> <a rel= "nofollow" class = "btn btn-primary btn-large" href= "#" >查看更多 ?</a> </p> </div> <h3>编辑</h3> <hr/> <!--留言提交--> <form action= "edit.php" method= "post" > <fieldset> <label>用户名</label><input type= "text" name= "username" class = "username" value= "<?php echo $editmsgs['username'];?>" required> <label>标题</label><input type= "text" name= "title" class = "title" value= "<?php echo $editmsgs['title'];?>" required> <label>内容</label><textarea name= "content" rows= "5" cols= "30" class = "content" value= "<?php echo $editmsgs['content'];?>" required></textarea><br/> <input type= "submit" class = "btn btn-primary btn-lg" name= "editMsg" value= "编辑完成" > <button><div class = "check" ><a href= "index.php" >查看留言</a></div></button> </fieldset> </form> </div> </div> </div> </body> </html> |
好帮手慕小尤
2019-08-18 11:24:48
同学你好,在index.php文件中为edit.php文件传递的参数名为id而不是editkey。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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 ); //反序列化字符串 } } // 获取数组的键名 $editkey = $_GET [ 'id' ]; $editmsgs = $msgs [ $editkey ]; var_dump( $editmsgs ); die ; |
祝学习愉快!
PHP小白零基础入门
- 参与学习 人
- 提交作业 626 份
- 解答问题 4928 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧