我不断得到一个致命的错误时,试图提交这个应用程序的信用卡支付信息,我在youtube上遵循

mmvthczy  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(452)

单击submit payment(提交付款)后出现错误:致命错误:在c:\xampp\htdocs\paypage\lib\pdo\u db.php的第37行调用null上的成员函数prepare()。需要帮忙吗?
费用.php

  1. <?php
  2. require_once('vendor/autoload.php');
  3. require_once('config/db.php');
  4. require_once('lib/pdo_db.php');
  5. require_once('models/Customer.php');
  6. require_once('models/Transaction.php');
  7. \Stripe\Stripe::setApiKey('sk_test_z2O6k7kNKzsBXFvLgcpx4FZq');
  8. // Sanitize POST Array
  9. $POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
  10. $first_name = $POST['first_name'];
  11. $last_name = $POST['last_name'];
  12. $email = $POST['email'];
  13. $token = $POST['stripeToken'];
  14. // Create Customer In Stripe
  15. $customer = \Stripe\Customer::create(array(
  16. "email" => $email,
  17. "source" => $token
  18. ));
  19. // Charge Customer
  20. $charge = \Stripe\Charge::create(array(
  21. "amount" => 5000,
  22. "currency" => "usd",
  23. "description" => "Intro to React Course",
  24. "customer" => $customer->id
  25. ));
  26. // Customer Data
  27. $customerData = [
  28. 'id' => $charge->customer,
  29. 'first_name' => $first_name,
  30. 'last_name' => $last_name,
  31. 'email' => $email
  32. ];
  33. // Instantiate Customer
  34. $customer = new Customer();
  35. // Add Customer To DB
  36. $customer->addCustomer($customerData);
  37. // Transaction Data
  38. $transactionData = [
  39. 'id' => $charge->id,
  40. 'customer_id' => $charge->customer,
  41. 'product' => $charge->description,
  42. 'amount' => $charge->amount,
  43. 'currency' => $charge->currency,
  44. 'status' => $charge->status,
  45. ];
  46. // Instantiate Customer
  47. $transaction = new Transaction();
  48. // Add Customer To DB
  49. $transaction->addTransaction($transactionData);
  50. // Redirect to success
  51. header('Location: success.php?tid='.$charge->id.'&product='.$charge->description);
  52. ?>

客户.php

  1. <?php
  2. require_once('config/db.php');
  3. require_once('lib/pdo_db.php');
  4. require_once('models/Customer.php');
  5. // Instantiate Customer
  6. $customer = new Customer();
  7. // Get Customer
  8. $customers = $customer->getCustomers();
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  16. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  17. <title>View Customers</title>
  18. </head>
  19. <body>
  20. <div class="container mt-4">
  21. <div class="btn-group" role="group">
  22. <a href="customers.php" class="btn btn-primary">Customers</a>
  23. <a href="transactions.php" class="btn btn-secondary">Transactions</a>
  24. </div>
  25. <hr>
  26. <h2>Customers</h2>
  27. <table class="table table-stripped">
  28. <thead>
  29. <tr>
  30. <th>Customer ID</th>
  31. <th>Name</th>
  32. <th>Email</th>
  33. <th>Date</th>
  34. </tr>
  35. </thead>
  36. <tbody>
  37. <?php foreach($customers as $c): ?>
  38. <td><?php echo $c->id; ?></td>
  39. <td><?php echo $c->first_name; ?> <?php echo $c->last_name; ?></td>
  40. <td><?php echo $c->email; ?></td>
  41. <td><?php echo $c->created_at; ?></td>
  42. <?php endforeach; ?>
  43. </tbody>
  44. </table>
  45. <br>
  46. <p><a href="index.php">Pay Page</a></p>
  47. </div>
  48. </body>
  49. </html>

事务处理.php

  1. <?php
  2. require_once('config/db.php');
  3. require_once('lib/pdo_db.php');
  4. require_once('models/Transaction.php');
  5. // Instantiate Transaction
  6. $transaction = new Transaction();
  7. // Get Transaction
  8. $transactions = $transaction->getTransactions();
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  16. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  17. <title>View Transactions</title>
  18. </head>
  19. <body>
  20. <div class="container mt-4">
  21. <div class="btn-group" role="group">
  22. <a href="customers.php" class="btn btn-secondary">Customers</a>
  23. <a href="transactions.php" class="btn btn-primary">Transactions</a>
  24. </div>
  25. <hr>
  26. <h2>Transactions</h2>
  27. <table class="table table-stripped">
  28. <thead>
  29. <tr>
  30. <th>Transaction ID</th>
  31. <th>Customer</th>
  32. <th>Product</th>
  33. <th>Amount</th>
  34. <th>Date</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <?php foreach($transactions as $t): ?>
  39. <tr>
  40. <td><?php echo $t->id; ?></td>
  41. <td><?php echo $t->customer_id; ?></td>
  42. <td><?php echo $t->product; ?></td>
  43. <td><?php echo sprintf('%.2f', $t->amount / 100); ?><?php echo strtoupper($t->currency); ?></td>
  44. <td><?php echo $t->created_at; ?></td>
  45. </tr>
  46. <?php endforeach; ?>
  47. </tbody>
  48. </table>
  49. <br>
  50. <p><a href="index.php">Pay Page</a></p>
  51. </div>
  52. </body>
  53. </html>

在客户和交易的模型文件夹中
客户.php

  1. <?php
  2. class Customer {
  3. private $db;
  4. public function __construct() {
  5. $this->db = new Database;
  6. }
  7. public function addCustomer($data) {
  8. // Prepare Query
  9. $this->db->query('INSERT INTO customers (id, first_name, last_name, email) VALUES(:id, :first_name, :last_name, :email)');
  10. // Bind Values
  11. $this->db->bind(':id', $data['id']);
  12. $this->db->bind(':first_name', $data['first_name']);
  13. $this->db->bind(':last_name', $data['last_name']);
  14. $this->db->bind(':email', $data['email']);
  15. // Execute
  16. if($this->db->execute()) {
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. }
  22. public function getCustomers() {
  23. $this->db->query('SELECT * FROM customers ORDER BY created_at DESC');
  24. $results = $this->db->resultset();
  25. return $results;
  26. }
  27. }

事务处理.php

  1. <?php
  2. class Transaction {
  3. private $db;
  4. public function __construct() {
  5. $this->db = new Database;
  6. }
  7. public function addTransaction($data) {
  8. // Prepare Query
  9. $this->db->query('INSERT INTO transactions (id, customer_id, product, amount, currency, status) VALUES(:id, :customer_id, :product, :amount, :currency, :status)');
  10. // Bind Values
  11. $this->db->bind(':id', $data['id']);
  12. $this->db->bind(':customer_id', $data['customer_id']);
  13. $this->db->bind(':product', $data['product']);
  14. $this->db->bind(':amount', $data['amount']);
  15. $this->db->bind(':currency', $data['currency']);
  16. $this->db->bind(':status', $data['status']);
  17. // Execute
  18. if($this->db->execute()) {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. public function getTransactions() {
  25. $this->db->query('SELECT * FROM transactions ORDER BY created_at DESC');
  26. $results = $this->db->resultset();
  27. return $results;
  28. }
  29. }

pdoèu db.php文件

  1. <?php
  2. /*
  3. * PDO DATABASE CLASS
  4. * Connects Database Using PDO
  5. * Creates Prepeared Statements
  6. * Binds params to values
  7. * Returns rows and results
  8. */
  9. class Database {
  10. private $host = DB_HOST;
  11. private $user = DB_USER;
  12. private $pass = DB_PASS;
  13. private $dbname = DB_NAME;
  14. private $dbh;
  15. private $error;
  16. private $stmt;
  17. public function __construct() {
  18. // Set DSN
  19. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  20. $options = array (
  21. PDO::ATTR_PERSISTENT => true,
  22. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  23. );
  24. // Create a new PDO instanace
  25. try {
  26. $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
  27. } // Catch any errors
  28. catch ( PDOException $e ) {
  29. $this->error = $e->getMessage();
  30. }
  31. }
  32. // Prepare statement with query
  33. public function query($query) {
  34. $this->stmt = $this->dbh->prepare($query);
  35. }
  36. // Bind values
  37. public function bind($param, $value, $type = null) {
  38. if (is_null ($type)) {
  39. switch (true) {
  40. case is_int ($value) :
  41. $type = PDO::PARAM_INT;
  42. break;
  43. case is_bool ($value) :
  44. $type = PDO::PARAM_BOOL;
  45. break;
  46. case is_null ($value) :
  47. $type = PDO::PARAM_NULL;
  48. break;
  49. default :
  50. $type = PDO::PARAM_STR;
  51. }
  52. }
  53. $this->stmt->bindValue($param, $value, $type);
  54. }
  55. // Execute the prepared statement
  56. public function execute(){
  57. return $this->stmt->execute();
  58. }
  59. // Get result set as array of objects
  60. public function resultset(){
  61. $this->execute();
  62. return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  63. }
  64. // Get single record as object
  65. public function single(){
  66. $this->execute();
  67. return $this->stmt->fetch(PDO::FETCH_OBJ);
  68. }
  69. // Get record row count
  70. public function rowCount(){
  71. return $this->stmt->rowCount();
  72. }
  73. // Returns the last inserted ID
  74. public function lastInsertId(){
  75. return $this->dbh->lastInsertId();
  76. }
  77. }

我想提醒大家,这是我在youtube上的第一个php项目,我之前没有php或mysql的经验。你在这里看到的一切都是在xampp上托管的。

gojuced7

gojuced71#

你验证过你的mysql示例在xampp中运行吗?

相关问题