Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Call to a member function prepare()
Forum PHP.pl > Forum > Przedszkole
kacper15
Witam forumowicze, mam mały problem i niestety nie wiem jak sobie z nim poradzić. Wstawiam klasy odpowiedzalne za rejestracje uzytkownika do bazy. Niestety nie wiem w czym jest problem i prosilbym o pomoc. W php tak na prawde dopiero raczkuje i sie ucze dlatego prosze o wyrozumiałość. W postmenie zwraca mi błąd : Call to a member function prepare()

  1. <?php
  2.  
  3. class Users{
  4. public $email;
  5. public $encrypted_password;
  6. public $username;
  7.  
  8. private $conn;
  9. private $user_table;
  10.  
  11. public function __construct($db){
  12. $this->conn = $db;
  13. $this->user_table = "users";
  14. }
  15. public function create_user()
  16. {
  17. $user_query = "'INSERT INTO users (username, email, encrypted_password) VALUES(?, ?, ?)'";
  18. $user_obj = $this->conn->prepare($user_query);
  19. $user_obj->bind_param("sss", $this->username, $this->email, $this->encrypted_password);
  20.  
  21. if($user_obj->execute()){
  22.  
  23. return true;
  24. }
  25. return false;
  26. }
  27. }
  28. ?>


  1. <?php
  2. class Database{
  3.  
  4. // specify your own database credentials
  5. private $host = "localhost";
  6. private $db_name = "spot";
  7. private $username = "root";
  8. private $password = "";
  9. public $conn;
  10.  
  11. // get the database connection
  12. public function getConnection(){
  13.  
  14. $this->conn = null;
  15.  
  16. try{
  17. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  18. $this->conn->exec("set names utf8");
  19. }catch(PDOException $exception){
  20. echo "Connection error: " . $exception->getMessage();
  21. }
  22.  
  23. return $this->conn;
  24. }
  25. }
  26. ?>
  27.  
  28.  


  1. <?php
  2.  
  3. header("Access-Control-Allow_Origin: *");
  4. header("Access-control_Allow-Methods: POST");
  5. header("Content-type: application/json; charst=UTF-8");
  6.  
  7. include_once 'database.php';
  8. include_once 'Users.php';
  9.  
  10.  
  11. $db = new Database();
  12. $connection = $db ->conn;
  13.  
  14.  
  15.  
  16. $user_obj = new Users($connection);
  17.  
  18. if($_SERVER['REQUEST_METHOD'] === "POST"){
  19. $data = json_decode(file_get_contents("php://input"));
  20. if(!empty($data->email) && !empty($data->encrypted_password) && !empty($data->username)){
  21. $user_obj->email = $data->email;
  22. $user_obj->encrypted_password = $data->encrypted_password;
  23. $user_obj->username = $data->username;
  24.  
  25.  
  26.  
  27. if($user_obj->create_user()){
  28. http_response_code(200);
  29. echo json_encode(array(
  30. "status" =>1,
  31. "message"=> "User has been created"
  32. ));
  33.  
  34. }else{
  35. http_response_code(500);
  36. echo json_encode(array(
  37. "status" =>0,
  38. "message" =>"Failed to save user"
  39. ));
  40. }
  41. }else{
  42. http_response_code(500);
  43. echo json_encode(array(
  44. "status" =>1,
  45. "message"=> "All data needed"
  46. ));
  47. }
  48. }else{
  49. http_response_code(503);
  50. echo json_encode(array(
  51. "status" =>0,
  52. "message" => "Access Denied"
  53. ));
  54. }
  55.  
  56.  
  57. ?>
viking
W konstruktorze Users daj var_dump($db). Zrób jakieś rzutowanie. \PDO $db
kacper15
Jak to rzutowanie? Nie za bardzo rozumiem ? ;/
viking
https://designpatternsphp.readthedocs.io/pl...ion/README.html

Poza tym mamy php 7.4 a tu nic z jego możliwości nie wykorzystujesz.
nospor
Zrobiles var_dump jak proszono?

Przenosze
kacper15
tak zrobilem ... W postmenie wyswietlilo mi sie ze wartosc jest NULL:


NULL
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\rest_api_spot\Users.php: 19
Stack trace:
#0 C:\xampp\htdocs\rest_api_spot\create-user-api.php(27): Users-&gt;create_user()
#1 {main
}
thrown in <b>C:\xampp\htdocs\rest_api_spot\Users.php</b> on line <b>19</b><br />
viking
Po linii z new PDO
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
nospor
zapomniales wywolac polaczenie....


  1. $db = new Database();
  2. $db->getConnection();
  3. $connection = $db ->conn;


edit: jak juz poprawisz to co napisalem, to zastanow sie czy oby na pewno o to zapytanie
$user_query = "'INSERT INTO users (username, email, encrypted_password) VALUES(?, ?, ?)'";
jest poprawne. Zastanow sie, czy nie dowaliles czegos tam totalnie bez sensu
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.