Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Problem z PDO
Osth
post 1.02.2013, 22:37:09
Post #1





Grupa: Zarejestrowani
Postów: 15
Pomógł: 0
Dołączył: 29.12.2012

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


Tak jak w temacie.

Jeżeli mam funkcję zaloguj(); to wyskakuje mi błąd
Cytat
Fatal error: Call to a member function prepare() on a non-object


a jeżeli zaloguj($db) to wyskakuje:
Cytat
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `id` = '(jakieś id)' AND `password` = '(jakieś hasło)''


Kod:
  1. <?
  2.  
  3. include 'config.php';
  4. global $db;
  5.  
  6. $zalogowany = $_SESSION['login'];
  7.  
  8. if(empty($_GET)) { index(); }
  9. if($_GET['cmd'] == 'zaloguj') { zaloguj($db); }
  10.  
  11.  
  12. function index() {
  13.  
  14. if($zalogowany == 'y') { panel(); }
  15. else {
  16. echo'<form action="login.php?cmd=zaloguj" method="POST">
  17. <input type="text" name="id"><br>
  18. <input type="password" name="password"><bR>
  19. <input type="submit"><br>';
  20. }
  21. }
  22.  
  23. function zaloguj($db) {
  24.  
  25. $stmt = $db -> prepare('SELECT id, password WHERE `id` = :id AND `password` = :password');
  26. $stmt->bindValue(':id', addslashes(htmlspecialchars($_POST['id'])), PDO::PARAM_INT);
  27. $stmt->bindValue(':password', addslashes(htmlspecialchars($_POST['password'])), PDO::PARAM_STR);
  28. $stmt->execute();
  29.  
  30. foreach ($stmt as $r) {
  31. $id_check = $r['id'];
  32. $password_check = $r['password'];
  33. }
  34.  
  35. if ($_POST['id'] == $id_check && $_POST['password'] == $password_check) {
  36. $_SESSION['login'] = 'y';
  37. print 'Zostałeś zalogowany.<br><a href="index.php">Index</a><br>';
  38. }
  39. else { print 'Podałeś złe dane.'; }
  40.  
  41. }
  42.  
  43. ?>
Go to the top of the page
+Quote Post
athei
post 1.02.2013, 23:49:03
Post #2





Grupa: Zarejestrowani
Postów: 389
Pomógł: 141
Dołączył: 11.04.2009

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


SELECT id, password FROM tabela WHERE

Ten post edytował athei 1.02.2013, 23:49:24
Go to the top of the page
+Quote Post
Osth
post 1.02.2013, 23:49:51
Post #3





Grupa: Zarejestrowani
Postów: 15
Pomógł: 0
Dołączył: 29.12.2012

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


Właśnie miałem pisać, żezauważyłem to wreszcie i skrypt śmiga elegancko biggrin.gif

Temat do zamknięcia wink.gif
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 13.07.2025 - 20:32