php - 当我尝试提交我在 YouTube 上关注的此应用程序的信用卡付款信息时,我不断收到 fatal error

标签 php mysql xampp

点击提交付款后出现错误: fatal error :在第 37 行 C:\xampp\htdocs\paypage\lib\pdo_db.php 中对 null 调用成员函数prepare()。请提供帮助吗?

收费.php

<?php
require_once('vendor/autoload.php');
require_once('config/db.php');
require_once('lib/pdo_db.php');
require_once('models/Customer.php');
require_once('models/Transaction.php');

\Stripe\Stripe::setApiKey('sk_test_z2O6k7kNKzsBXFvLgcpx4FZq');

// Sanitize POST Array
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

$first_name = $POST['first_name'];
$last_name = $POST['last_name'];
$email = $POST['email'];
$token = $POST['stripeToken'];

// Create Customer In Stripe 
$customer = \Stripe\Customer::create(array(
    "email" => $email,
    "source" => $token
));

// Charge Customer
$charge = \Stripe\Charge::create(array(
    "amount" => 5000,
    "currency" => "usd",
    "description" => "Intro to React Course",
    "customer" => $customer->id
));

// Customer Data
$customerData = [
    'id' => $charge->customer,
    'first_name' => $first_name,
    'last_name' => $last_name,
    'email' => $email
];

// Instantiate Customer
$customer = new Customer();

// Add Customer To DB
$customer->addCustomer($customerData);

// Transaction Data
$transactionData = [
    'id' => $charge->id,
    'customer_id' => $charge->customer,
    'product' => $charge->description,
    'amount' => $charge->amount,
    'currency' => $charge->currency,
    'status' => $charge->status,
];

// Instantiate Customer
$transaction = new Transaction();

// Add Customer To DB
$transaction->addTransaction($transactionData);

// Redirect to success
header('Location: success.php?tid='.$charge->id.'&product='.$charge->description);
?>

客户.php

<?php
require_once('config/db.php');
require_once('lib/pdo_db.php');
require_once('models/Customer.php');

// Instantiate Customer
$customer = new Customer();

// Get Customer
$customers = $customer->getCustomers();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<title>View Customers</title>
</head>
<body>
    <div class="container mt-4">
        <div class="btn-group" role="group">
            <a href="customers.php" class="btn btn-primary">Customers</a>
            <a href="transactions.php" class="btn btn-secondary">Transactions</a>
    </div>
    <hr>
    <h2>Customers</h2>
    <table class="table table-stripped">
        <thead>
            <tr>
                <th>Customer ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($customers as $c): ?>
                <td><?php echo $c->id; ?></td>
                <td><?php echo $c->first_name; ?> <?php echo $c->last_name; ?></td>
                <td><?php echo $c->email; ?></td>
                <td><?php echo $c->created_at; ?></td>
            <?php endforeach; ?>
        </tbody>
    </table>
    <br>
    <p><a href="index.php">Pay Page</a></p>
</div>
</body>
</html>

交易.php

<?php
require_once('config/db.php');
require_once('lib/pdo_db.php');
require_once('models/Transaction.php');

// Instantiate Transaction
$transaction = new Transaction();

// Get Transaction
$transactions = $transaction->getTransactions();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<title>View Transactions</title>
</head>
<body>
<div class="container mt-4">
    <div class="btn-group" role="group">
        <a href="customers.php" class="btn btn-secondary">Customers</a>
        <a href="transactions.php" class="btn btn-primary">Transactions</a>
    </div>
    <hr>
    <h2>Transactions</h2>
    <table class="table table-stripped">
        <thead>
            <tr>
                <th>Transaction ID</th>
                <th>Customer</th>
                <th>Product</th>
                <th>Amount</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($transactions as $t): ?>
                <tr>
                    <td><?php echo $t->id; ?></td>
                    <td><?php echo $t->customer_id; ?></td>
                    <td><?php echo $t->product; ?></td>
                    <td><?php echo sprintf('%.2f', $t->amount / 100); ?><?php echo strtoupper($t->currency); ?></td>
                    <td><?php echo $t->created_at; ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <br>
    <p><a href="index.php">Pay Page</a></p>
</div>
</body>
</html>

在客户和交易的模型文件夹内

客户.php

<?php
class Customer {
    private $db;

    public function __construct() {
        $this->db = new Database;
    }

    public function addCustomer($data) {
        // Prepare Query
        $this->db->query('INSERT INTO customers (id, first_name, last_name, email) VALUES(:id, :first_name, :last_name, :email)');

        // Bind Values
        $this->db->bind(':id', $data['id']);
        $this->db->bind(':first_name', $data['first_name']);
        $this->db->bind(':last_name', $data['last_name']);
        $this->db->bind(':email', $data['email']);

        // Execute
        if($this->db->execute()) {
            return true;
        } else {
            return false;
        }
    }

    public function getCustomers() {
        $this->db->query('SELECT * FROM customers ORDER BY created_at DESC');

        $results = $this->db->resultset();

        return $results;
    }
}

交易.php

<?php
    class Transaction {
        private $db;

        public function __construct() {
            $this->db = new Database;
        }

        public function addTransaction($data) {
            // Prepare Query
            $this->db->query('INSERT INTO transactions (id, customer_id, product, amount, currency, status) VALUES(:id, :customer_id, :product, :amount, :currency, :status)');

            // Bind Values
            $this->db->bind(':id', $data['id']);
            $this->db->bind(':customer_id', $data['customer_id']);
            $this->db->bind(':product', $data['product']);
            $this->db->bind(':amount', $data['amount']);
            $this->db->bind(':currency', $data['currency']);
            $this->db->bind(':status', $data['status']);

            // Execute
            if($this->db->execute()) {
                return true;
            } else {
                return false;
            }
        }

        public function getTransactions() {
            $this->db->query('SELECT * FROM transactions ORDER BY created_at DESC');

            $results = $this->db->resultset();

            return $results;
        }
    }

pdo_db.php

<?php
    /* 
     *  PDO DATABASE CLASS
     *  Connects Database Using PDO
     *  Creates Prepeared Statements
     *  Binds params to values
     *  Returns rows and results
     */
class Database {
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() {
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        $options = array (
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
        );
        // Create a new PDO instanace
        try {
            $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
        }       // Catch any errors
        catch ( PDOException $e ) {
            $this->error = $e->getMessage();
        }
    }

    // Prepare statement with query
    public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
    }

    // Bind values
    public function bind($param, $value, $type = null) {
        if (is_null ($type)) {
            switch (true) {
                case is_int ($value) :
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool ($value) :
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null ($value) :
                    $type = PDO::PARAM_NULL;
                    break;
                default :
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    // Execute the prepared statement
    public function execute(){
        return $this->stmt->execute();
    }

    // Get result set as array of objects
    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_OBJ);
    }

    // Get single record as object
    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_OBJ);
    }

    // Get record row count
    public function rowCount(){
        return $this->stmt->rowCount();
    }

    // Returns the last inserted ID
    public function lastInsertId(){
        return $this->dbh->lastInsertId();
    }
}

我想提醒大家,这是我在 youtube 上关注的第一个 php 项目,之前没有 PHP 或 MySql 经验。您在这里看到的所有内容都托管在 XAMPP 上。

最佳答案

您是否已验证您的 MySQL 实例是否在 XAMPP 中运行?

关于php - 当我尝试提交我在 YouTube 上关注的此应用程序的信用卡付款信息时,我不断收到 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49911267/

相关文章:

php - mysql得到第4、5、6、7个结果

MySQL - 联合还是加入?

php - 放在服务器上后更新单条记录解析错误?

java - 使用 <a> 标签在 jsp 页面中链接 php 页面

php - 使用表名输入的最安全方法?

javascript - PHP 作为 Javascript,获取参数

php - XDebug 可以调试使用 pcntl_fork() 的 PHP CLI 脚本吗?

php - mysqli_stmt_bind_result 的奇怪问题

apache - JOOMLA 网站速度太慢

Java XAMPP 连接性