数组报错 array_push() expects parameter 1 to be array

新手上路,请多包涵

图片描述

在第20行出现了报错

下面是这个php文件的完整代码:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$file="123.txt";
$msgs=[];
//检测文件是否存以及读取
if (file_exists($file)){
    $string=file_get_contents($file);            //为真则读取文件并赋值
    if (strlen($string)>0){                      //判断获取的文件是否有内容
        $msgs=unserialize($string);              //有则转为数组并赋值
    };
};
//判断提交是否正常以及接收
if (isset($_POST['subpop'])){
    $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);/*      报错提示如下
           Warning: array_push() expects parameter 1 to be array, null given in / on line 20
                                    */
    $msgs=serialize($msgs);                      //重新转换并赋值
    if(file_put_contents($file,$msgs)){                 //将$msgs压入文件末行
        echo "<script>alert('留言成功!');</script>";
    }else {
        echo "<script>alert('留言失败!');</script>";
    };
};
?>


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <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/bootstrap.min.js"></script>
    <title>Document</title>
    <style>
        .Dcenter{
            position: fixed;
            bottom: 0;
        }
    </style>
</head>
<body>
<?php if(is_array($msgs)&&count($msgs)>0):?>

    <table cellspacing="0" cellpadding="0" width="70%" class="table table-sm table-bordered">
        <tr>
            <th>用户名:</th>
            <th>标题:</th>
            <th>内容:</th>
            <th>时间:</th>
        </tr>
        <tbody>
        <?php $i=1;foreach($msgs as $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>
            </tr>
        <?php endforeach;?>
        </tbody>
    </table>
<?php endif;?>

<div class="container">
    <div class="row Dcenter">
        <form action="#" method="post">
            <fieldset>
                <legend>留言板</legend>
                <label>用户名:</label><input type="text" name="username" placeholder="请输入用户名" required>
                <label>标题:</label><input type="text" name="title" placeholder="请输入标题" required>
                <label>内容:</label><textarea name="content" rows="1"></textarea>
                <input type="submit" name="subpop" value="提交">
            </fieldset>
        </form>
    </div>
</div>
</body>
</html>
阅读 15.3k
1 个回答

$msg没数组 报错信息 写了 array_push第一个参数要 数组 你给了null

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题