douzhan8652 2015-06-20 11:27
浏览 70
已采纳

致命错误:在非对象上调用成员函数setAttribute()

I am trying to setup a newseltter, but in my newsletter class I keep getting the error: Fatal error: Call to a member function setAttribute() on a non-object

I have been converting the file from an older non pdo version.

<?php
class NEWSLETTER{
    private static $email;
    private static $datetime = null;
    private static $valid = true;

        function __construct($conn){
        $this->db = $conn;
    }

    public function signup($email){
        if(!empty($_POST)){
            self::$email = $_POST['email'];
            self::$datetime = date('Y-m-d H:i:s');

            if(empty(self::$email)){
                $status = "Error";
                $message = "No email entered.";
                self::$vaild = false;
            }else if(!filter_var(self::$email, FILTER_VALIDATE_EMAIL)){
                $status = "Error";
                $message = "Vaild email required.";
                self::$valid = false;
            }

            if(self::$valid){
                $this->db->setAttribute(PDO::ATTR_ERRMODE_EXCEPTION);
                $exist = $this->prepare("SELECT COUNT (*) FROM newsletter WHERE email='$email'");
                $exist->execute();
                $data_exists = ($exist->fetchColumn() > 0) ? true : false;

                if(!$data_exists){
                    $sql = "INSERT INTO newsletter (email, date) VALUES (:email, :datetime)";
                    $q = $pdo->prepare($sql);
                    $q->execute(array(':email'=>self::$email, ':datetime'=>self::$datetime));

                    if($q){
                        $status = "Success";
                        $message = "You have been subscribed to the Epic Owl newsletter.";
                    }else{
                        $status = "Error";
                        $message = "An error occurred, try again.";
                    }
                }else{
                    $status = "Error";
                    $message = "You have already subscribed at an earlier date.";
                }
            }

            $data = array('status' => $status, 'message' => $message);

            echo json_encode($data);
        }
    }
}
?>

 <?php
ini_set('display_errors', '1');
require_once './includes/conn.php';

if(!empty($_POST['email'])){
    $email = $_POST['email'];

    $newsletter->signup($email);
}
?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>EpicOwl UK | CMS Admin Panel Mail List</title>
        <meta charset="utf-8">
        <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" href="../css/main.css">
    </head>
    <body>
    <div id="header">
        <a href="index.php"><img id="logo" src="../images/logo.png" /></a>
        <div id="navigation">
            <ul>
                <a href="../index.php"><li>Home</li></a>
                <a href="./index.php"><li>Admin Panel</li></a>
            </ul>
        </div>
    </div>
    <div id="content">
    <form method="post">
        <br /><h2>Signup to the Epic Owl Newsletter(NOT WORKING! STILL BEING DEVELOPED!)</h2>
        <input type="text" name="email" placeholder="Your Email Address" /><br /><br />
        <input type="submit" name="submit" value="Signup" /><br /><br /><br /><br />
    </form>

            <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
            <script src="./includes/js/lib.js"></script>
    </div>
    <div id="footer">
        <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
    </div>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • douxing2156 2015-06-20 11:43
    关注

    $pdo is not defined anywhere in your class. You should call setAttribute() and prepare() on your db connection, i.e. $this->db->setAttribute() provided $this->db is actually a valid connection to a DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?