请教!这是个什么错误

来源:2-12 留言板功能实现

qq_ionicRoyKent_04116743

2017-04-25 15:25:33

http://climg.mukewang.com/58fef9b90001855f10660182.jpghttp://climg.mukewang.com/58fef9e000017e8e05170050.jpg

写回答

3回答

好帮手慕查理

2017-04-27

您好,您的代码我们已经运行,并没有再报waring错误,请您检查一下其它代码。

0
huly
回复
hu_zhous
h 你好,建议打印检查$msgs是否为空数组,如果为空,添加一条留言,看是否还出现同样问题,如果解决了您的问题请采纳,祝学习愉快!
h017-05-19
共4条回复

qq_ionicRoyKent_04116743

提问者

2017-04-25

header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs = array();
//检测文件是否存在
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);
  if(file_put_contents($filename,$msgs)){
    echo "<script>alert('留言成功!');location.href='22-msg.php';</script>";
  }else{
    echo "<script>alert('留言失败!');location.href='22-msg.php';</script>";
  }
}
/*

我写的和讲师是一样的代码!为什么我会报这个错误

0

好帮手慕查理

2017-04-25

您好,第一个参数$msgs不是数组,所以报Warning错误。array_push()函数第一个参数为数组。如果解决了您的问题,请采纳,祝学习愉快!

0
hq_ionicRoyKent_04116743
h header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC'); $filename="msg.txt"; $msgs = array(); //检测文件是否存在 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); if(file_put_contents($filename,$msgs)){ echo "<script>alert('留言成功!');location.href='22-msg.php';</script>"; }else{ echo "<script>alert('留言失败!');location.href='22-msg.php';</script>"; } } 代码和讲师的是一样的可是报错了,不知道怎么回事
h017-04-25
共1条回复

0 学习 · 2569 问题

查看课程