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 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题