编辑功能只能编辑第二条数据,麻烦老师帮忙看一下

编辑功能只能编辑第二条数据,麻烦老师帮忙看一下

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>&nbsp;&nbsp;|&nbsp;&nbsp;
 <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>

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

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

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

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

点击第四条数据后出现第二条数据的标题和用户名,没有内容。编辑内容后跳转index页后发现第二条数据被修改,添加数据和删除数据功能都没问题

正在回答

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

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 提问者 #1
    好了,谢谢老师
    2019-08-18 16:51:35
好帮手慕小尤 2019-08-18 14:06:37

同学你好,测试代码是可以完成编辑的。同学可以将数据全部删除,然后重新添加数据。

textarea标签的默认值不是写在value中的,而是现在标签中间。代码如下:

1
2
<label>内容</label>
<textarea name="content" rows="5" cols="30" class="content" required><?php echo $editmsgs['content'];?></textarea><br/>

祝学习愉快!

  • 提问者 断线纸鸢1 #1
    老师,我代码改成了这样: <label>用户名</label><input type="text" name="username" class="username" required value="<?php echo $editmsgs['username'];?>"> <label>标题</label><input type="text" name="title" class="title" required value="<?php echo $editmsgs['title'];?>"> <label>内容</label><textarea name="content" rows="5" cols="30" class="content" required><?php echo $editmsgs['content'];?></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> 删除msgs.txt文件后重新添加了一条留言,点击编辑发现会重新添加一条留言,第二条留言可以编辑,编辑之后添加的所有留言都只能编辑第二条。显示未定义的索引id在edit.php18行
    2019-08-18 16:05:00
提问者 断线纸鸢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>

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


好帮手慕小尤 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;

祝学习愉快!

  • 提问者 断线纸鸢1 #1
    老师,我按你的代码修改后发现 //获取数组的键名 $editkey=$_GET['id']; $editmsgs=$msgs[$editkey]; var_dump($editmsgs);die; 点击index.php的编辑功能跳转后只有我点击的那条数据的数组出现,如: array(4) { ["username"]=> string(4) "Nova" ["title"]=> string(6) "你好" ["content"]=> string(6) "你好" ["time"]=> int(1566043208) } 去掉die;之后点击编辑虽然能跳转到相应的数据编辑页面,但是内容还是没有,还是只能修改第二条数据
    2019-08-18 11:58:35
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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