Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PDO MySQL Insert, Błędne zapytanie
redirdoolb
post
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 3.05.2019

Ostrzeżenie: (0%)
-----


Hej, mam problem z zapytaniem, nie mogę sprawić aby przeszło pomyślnie.

Dane wejściowe:
  1. (
  2. [table] => players
  3. [fields] => Array
  4. (
  5. [0] => name
  6. [1] => password
  7. [2] => email
  8. )
  9.  
  10. [data] => Array
  11. (
  12. [:name] => tester
  13. [:password] => 72a3dcef165d9122a45decf13ae20631
  14. [:email] => test@test.pl
  15. )
  16.  
  17. )


Oczywiście dane są odpowiednio wrzucane do poniższej funkcji:

  1. function insert($table = "", $fields = [], $data = []){
  2. if($table == ""){
  3. pr("First variable (table) is required and cannot be empty.");
  4. } elseif(count($fields)==0) {
  5. pr("Fields are not defined.");
  6. } elseif(count($data)==0) {
  7. pr("Data is not defined.");
  8. } else {
  9. $fields = implode(",", $fields);
  10.  
  11. $placeholders = [];
  12.  
  13. foreach($data as $placeholder => $value){
  14. $placeholders[] = $placeholder;
  15. }
  16. $placeholders = implode(",", $placeholders);
  17.  
  18. $stmt = $this->connection->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");
  19.  
  20. foreach($data as $placeholder => $value){
  21. $bind = $stmt->bindValue($placeholder, $value);
  22. }
  23.  
  24. if($stmt->execute()){
  25. return true;
  26. } else {
  27. return false;
  28. }
  29. }
  30. }


Zapytanie które widzę po użyciu "print_r" to:
  1. INSERT INTO players (name,password,email) VALUES (:name,:password,:email)


Zapytanie które powinno się wykonać:
  1. INSERT INTO players (name,password,email) VALUES ("tester", "72a3dcef165d9122a45decf13ae20631", "test@test.pl")


Wprowadziłem zapytanie ręcznie i wszystko działa, błąd jest prawdopodobnie z bindowaniem wartości.

Na funkcję "pr" nie musicie patrzeć, to po prostu print_r otoczony <pre></pre>, używam go do podglądu wartości.

Pozdrawiam,
redirdoolb

Ten post edytował redirdoolb 3.05.2019, 13:38:36
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 18.09.2025 - 13:50