【问题标题】:Call to a member function bindParam() on null在 null 上调用成员函数 bindParam()
【发布时间】:2017-10-16 08:35:53
【问题描述】:

我创建了一个页面,该页面应该在我的数据库中添加新条目,当我尝试上传文件时收到此消息:

在 null 上调用成员函数 bindParam()

我检查了我的代码,在我的连接中看不到任何错误。

<?php

    error_reporting( ~E_NOTICE ); // avoid notice

    require_once 'dbconfig.php';

    if(isset($_POST['btnsave']))
    {
        $username = $_POST['user_name'];// user name
        $userjob = $_POST['user_job'];// user email

        $imgFile = $_FILES['user_image']['name'];
        $tmp_dir = $_FILES['user_image']['tmp_name'];
        $imgSize = $_FILES['user_image']['size'];


        if(empty($username)){
            $errMSG = "Please Enter Username.";
        }
        else if(empty($userjob)){
            $errMSG = "Please Enter Your Job Work.";
        }
        else if(empty($imgFile)){
            $errMSG = "Please Select Image File.";
        }
        else
        {

            $upload_dir = 'user_images/'; // upload directory

            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

            // valid image extensions
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'pdf', 'docx'); // valid extensions

            // rename uploading image
            $userpic = rand(1000,1000000).".".$imgExt;

            // allow valid image file formats
            if(in_array($imgExt, $valid_extensions)){           
                // Check file size '5MB'
                if($imgSize < 5000000)              {
                    move_uploaded_file($tmp_dir,$upload_dir.$userpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
            }
        }


        // if no error occured, continue ....
        if(!isset($errMSG))
        {

            $sql = "INSERT INTO user (fname, lname, memo, file) VALUES (:fname, :lname, :memo, :file) ON DUPLICATE KEY UPDATE memo=:memo2";
            $stmt->bindParam(':fname',$name);
            $stmt->bindParam(':lname',$lname);
            $stmt->bindParam(':memo',$memo);
            $stmt->bindParam(':file',$file);
            $stm->execute(array(":fname" => $fname, ":hash" => $hash, ":memo" => $memo, ":memo2" => $memo));

            if($stmt->execute())
            {
                $successMSG = "new record succesfully inserted ...";
                header("refresh:5;index.php"); // redirects image view page after 5 seconds.
            }
            else
            {
                $errMSG = "error while inserting....";
            }
        }
    }
?>

【问题讨论】:

    标签: php mysqli pdo data-binding prepared-statement


    【解决方案1】:

    你从来没有准备过。 $stmt 从未声明过。这就是null 警告的原因。

    if($stmt=$conn->prepare(
          "INSERT INTO user (fname, lname, memo, file) VALUES (:fname, :lname, :memo, :file) ON DUPLICATE KEY UPDATE memo=:memo2"
        )
    ){
        // ... make sure you bind :memo2 as well.
        // don't load values into execute()
        // remove this line:  $stm->execute(array(":fname" => $fname, ":hash" => $hash, ":memo" => $memo, ":memo2" => $memo));
        // use if($stmt->execute()){...
    }
    

    【讨论】:

    • 试过了但是你说了还是不行,不好意思问了这么多问题,我刚开始学php
    • @JayDoe 请在我的代码块中重新阅读我的 cmets。并参考我的相关答案的超链接。您不需要将查询指定为 $sql,只需在准备调用中使用它即可。不要使用那个狡猾的 $stm 行,使用 if($stmt-&gt;execute()){... 你需要将 $memo2 绑定到 :memo2
    • 感谢您的帮助,但我尝试了另一种方法,它有效,但只有一个问题。我不知道我能不能问你这个,我想我只是把它贴在另一个线程上。再次感谢您
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2017-08-09
    • 2012-09-29
    • 2020-03-10
    • 2017-09-24
    • 2018-02-06
    • 2018-09-29
    相关资源
    最近更新 更多