duankaolei2921 2019-07-24 18:24
浏览 117

问题得到函数结果回声

I just started learning more about functions and am currently stuck on one part of them. I am trying to get the result of the function that I set in the return to populate in html in a specific spot.

Does anyone see what I am doing wrong?

Here is the error message:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function profilePic(), 0 passed in /adminNav.php on line 76 and exactly 1 expected in /adminNav.php:6 Stack trace:

0 /adminNav.php(76): profilePic()

1 /admin/project-gallery.php(93): include('pub...')

2 {main} thrown in /adminNav.php on line 6

Full code:

function profilePic($con) {
    try {
        //Profile pic in main
        $sqlProfileImg = "
            SELECT *
            FROM profile_img
            WHERE user_id = ?
            ORDER BY id DESC
            LIMIT 1
            ";
        if ($stmtProfileImg = $con->prepare($sqlProfileImg)) {
            $stmtProfileImg->execute(array($user_id));

            $default_profile_img = NULL;
            $userProfilePic = NULL;
            $profilePic = NULL;
            $profilePicRows = $stmtProfileImg->fetchAll(PDO::FETCH_ASSOC);
            foreach ($profilePicRows as $profilePicRow) {
                $profilePic = $profilePicRow['img'];
                $profilePic = (!empty($profilePic) && $profilePic != 0)? $profilePic : "profile_images/default.jpg"; ?>" alt="<?php echo (!empty($profilePic) && $profilePic != 0)? "Profile Picture" : "No Picture";
            }
        }
    } catch (PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
        $date = new DateTime();
        file_put_contents('error_log_profile_pic', "
[{$date->format('Y-m-d H:i:s')}]" . "Error adding attachment: 
" . print_r($e, 1), FILE_APPEND);
        return false;
    }
    return $profilePic;
}

HTML

<img id="navUserProfilePic" src="<?php profilePic(); ?>">
  • 写回答

2条回答 默认 最新

  • dongwuli5105 2019-07-24 18:27
    关注

    You need to pass a parameter to that function

    For example, like this

    <img id="navUserProfilePic" src="<?php echo profilePic($con); ?>">
                                           ^^^^            ^^^^
    

    Your other issue is SCOPE. The function know's nothing about a variable called $user_id

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)