Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [MySQL][PHP]Bindowanie!, pdo
woxala123
post
Post #1





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Witam!
Mam takie zapytanie czy w tym kodzie który jest poniżej muszę coś bindować?

  1.  
  2. $query = $pdo->prepare("SELECT * FROM users ORDER BY id");
  3. $query ->execute();
  4. $result = $query->setFetchMode(PDO::FETCH_ASSOC);
  5. $i = 0;
  6. echo '<table width="50%">';
  7. echo '<tr>';
  8. echo '<td>ID</td>';
  9. echo '<td>User</td>';
  10. echo '<td>Password</td>';
  11. echo '</tr>';
  12.  
  13. echo "<form name='form_update' method='post' action='upa2.php'>\n";
  14. while ($users = $query->fetch(PDO::FETCH_BOTH)) {
  15. echo '<tr>';
  16. echo "<td>{$users['id']}<input type='hidden' name='id[$i]' value='{$users['id']}' /></td>";
  17. echo "<td>{$users['username']}</td>";
  18. echo "<td><input type='text' size='40' name='password[$i]' value='{$users['password']}' /></td>";
  19. echo "<td><input type='text' size='40' name='username[$i]' value='{$users['username']}' /></td>";
  20.  
  21. echo '</tr>';
  22. ++$i;
  23. }
  24. echo '<tr>';
  25. echo "<td><input type='submit' value='submit' /></td>";
  26. echo '</tr>';
  27. echo "</form>";
  28. echo '</table>';
  29.  
  30.  

Dopiero bindowanie robie w drugim pliku upa2.php i pytanie czy dobrze jest zrobione bindowanie?
  1. $username= $_POST['username'][$i];
  2. $password= $_POST['password'][$i];
  3. $id = $_POST['id'][$i];
  4.  
  5. $sql = "UPDATE users SET password = '$password' , username = '$username' WHERE id = '$id' LIMIT 1";;
  6. $stmt = $pdo->prepare($sql);
  7. $stmt->bindParam(':username', $_POST['$username'], PDO::PARAM_STR);
  8. $stmt->bindParam(':password', $_POST['$password'], PDO::PARAM_STR);
  9. $stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
  10. $stmt->execute();
  11.  


Ten post edytował woxala123 3.02.2017, 22:00:47
Go to the top of the page
+Quote Post
viking
post
Post #2





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


I gdzie masz niby to bindowanie w update?


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #3





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


CZyli to powinno być tak?
  1. $stmt->bindValue(':username', $username, PDO::PARAM_STR);
  2. $stmt->bindValue(':password', $password, PDO::PARAM_STR);
  3. $stmt->bindValue(':id', $id, PDO::PARAM_INT);
  4.  


Ten post edytował woxala123 3.02.2017, 22:42:17
Go to the top of the page
+Quote Post
viking
post
Post #4





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Czyli najpierw ten placeholder musi się znajdować gdzieś w zapytaniu. Masz wszystko w dokumentacji opisane.


--------------------
Go to the top of the page
+Quote Post
Niree
post
Post #5





Grupa: Zarejestrowani
Postów: 220
Pomógł: 18
Dołączył: 5.02.2016
Skąd: Polska

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


  1. while ($users = $query->fetch(PDO::FETCH_BOTH)) {
  2. echo '<tr>';
  3. echo "<td>{$users['id']}<input type='hidden' name='id[$i]' value='{$users['id']}' /></td>";
  4. echo "<td>{$users['username']}</td>";
  5. echo "<td><input type='text' size='40' name='password[$i]' value='{$users['password']}' /></td>";
  6. echo "<td><input type='text' size='40' name='username[$i]' value='{$users['username']}' /></td>";
  7.  
  8. echo '</tr>';
  9. ++$i;
  10. }


Przecież pętla while wyciąga każdy rekord z bazy i wykonuje się tyle razy, ile jest rekordów. Po co każdy jeszcze indeksujesz?
Go to the top of the page
+Quote Post
woxala123
post
Post #6





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Przecież tu jest to w zapytaniu
  1. $sql = "UPDATE users SET password = '$password' , username = '$username' WHERE id = '$id' LIMIT 1";;
  2. $stmt = $pdo->prepare($sql);


Niree dlatego tak to robię bo działa. Tylko jeszcze chce wiedzieć czy dobrze to zrobiłem bindowanie?

  1. $sql = "UPDATE users SET password = '$password' , username = '$username' WHERE id = '$id' LIMIT 1";;
  2. $stmt = $pdo->prepare($sql);
  3. $stmt->bindValue(':username', $username, PDO::PARAM_STR);
  4. $stmt->bindValue(':password',$password, PDO::PARAM_STR);
  5. $stmt->bindValue(':id', $id, PDO::PARAM_INT);
  6. $stmt->execute();
  7.  
  8.  

To jest moja wersja. Czy ona będzie okey?
Go to the top of the page
+Quote Post
viking
post
Post #7





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Obrazowo. Zabierasz się za malowanie pokoju. Przygotowałes farbę, umoczyles wałek a na ścianach obrazy, na podłodze dywan i meble rozłożone. Do tego drzwi do pokoju otwarte i każdy może wejść i wszystko wynieść. Tak wygląda twoje zapytanie.


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #8





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Viking to gdzie jest może błąd?
Na końcu kodu dodaje
  1. $stmt->closeCursor();
  2. unset($stmt);
  3. $pdo = null;


Ten post edytował woxala123 3.02.2017, 23:40:37
Go to the top of the page
+Quote Post
viking
post
Post #9





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Patrzyłeś w dokumentację czy jak zawsze ci się nie chce. Wklej tutaj przykładowe zapytanie które podają.


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #10





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Tutaj mam taki przykład

  1. if($_POST['action'] == 'update')
  2. {
  3. $id = $_POST['id'];
  4.  
  5. $password = $_POST['password'];
  6. $first_name = $_POST['first_name'];
  7. $last_name = $_POST['last_name'];
  8. $email = $_POST['email'];
  9. $country = $_POST['country '];
  10. $city = $_POST['city'];
  11. $post_code = $_POST['post_code'];
  12. $address = $_POST['address'];
  13. $phone = $_POST['phone'];
  14. $updated_at = time();
  15.  
  16. // przygotowanie szkieletu zapytania
  17. $stmt = $dbh->prepare('UPDATE customers SET
  18. password = :password,
  19. first_name = :first_name,
  20. last_name = :last_name,
  21. email = :email,
  22. country = :country,
  23. city = :city,
  24. post_code = :post_code,
  25. address = :address,
  26. phone = :phone,
  27. updated_at = :updated_at
  28. WHERE customer_id = :id
  29. ');
  30.  
  31. // przypisujemy zmienne do placeholderów
  32.  
  33. bindValue(':login', $login, PDO::PARAM_STR);
  34. bindValue(':password', $password, PDO::PARAM_STR);
  35. bindValue(':first_name', $first_name, PDO::PARAM_STR);
  36. bindValue(':last_name', $last_name, PDO::PARAM_STR);
  37. bindValue(':email', $email, PDO::PARAM_STR);
  38. bindValue(':country', $country , PDO::PARAM_STR);
  39. bindValue(':city', $city, PDO::PARAM_STR);
  40. bindValue(':post_code', $post_code, PDO::PARAM_STR);
  41. bindValue(':address', $address, PDO::PARAM_STR);
  42. bindValue(':phone', $phone, PDO::PARAM_STR);
  43. bindValue(':updated_at', $updated_at, PDO::PARAM_INT);
  44. bindValue(':id', $id, PDO::PARAM_INT);
  45.  
  46. // wykonujemy zapytanie
  47. $result = $stmt->execute();
  48.  
  49. if($result !== false)
  50. {
  51. echo 'Zaktualizowano użytkownika o ID = ' . $id;
  52. } else {
  53. echo 'Wystąpił błąd';
  54. }
  55.  
  56. $stmt->closeCursor();
  57. unset($stmt);
  58. $dbh = null;
  59. }
  60.  
Go to the top of the page
+Quote Post
viking
post
Post #11





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


I naprawdę nie widzisz różnicy w kodzie prepare obu zapytań?


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #12





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


no okey poprawie to i ocenisz
  1. $stmt = $pdo->prepare( "UPDATE users SET password = '$password' , username = '$username' WHERE id = '$id' LIMIT 1");
  2. $stmt->bindValue(':username', $username, PDO::PARAM_STR);
  3. $stmt->bindValue(':password',$password, PDO::PARAM_STR);
  4. $stmt->bindValue(':id', $id, PDO::PARAM_INT);
  5. $result = $stmt->execute();
  6.  


Ten post edytował woxala123 4.02.2017, 00:00:30
Go to the top of the page
+Quote Post
Tomplus
post
Post #13





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


@woxala123

Viking próbuje Cię naprowadzić na rozwiązanie, ale chyba masz złe okulary na nosie i nie widzisz.

Jeżeli stosujesz bindValue to zgodnie z twoimi zmiennymi zapytanie do bazy powinno wyglądać tak:

Kod
UPDATE users SET password = :password , username = :username WHERE id = :id


Bez cudzysłowia, bez dolców. Jak się zagłębisz w zapytania PDO, to będziesz mógł tworzyć zapytania bez zmiennych bind podanych wyżej, a tylko:

Kod
UPDATE users SET password = ? , username = ? WHERE id = ?


LIMIT 1 - jeżeli kolumna ID jest unikatowa, jest nie potrzebne.
Go to the top of the page
+Quote Post
viking
post
Post #14





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


I popsułeś zabawę smile.gif Zaraz pewnie pojawi się 100 innych podobnych pytań. Voxala musi się nauczyć sam dochodzić do rozwiązania bo dotychczasowe jego problemy wynikają z nieumiejętnego czytania dokumentacji i przykładów.


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #15





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Zabawa jest świetna. Ale skoro zastosowałem zmienne to je bindowałem.Twój przykład też jest godny zastosowania-też już czytałem na ten temat. To jest tylko kwestia przerobienia kodu.
teraz wygląda tak. Dostoswałem się jak jest wyżej.

  1. $stmt = $pdo->prepare( "UPDATE users SET password = :password , username = :username WHERE id = :id");
  2.  
  3. $stmt->bindParam(':username', $username, PDO::PARAM_STR);
  4. $stmt->bindParam(':password',$password, PDO::PARAM_STR);
  5. $stmt->bindParam(':id', $id, PDO::PARAM_INT);
  6. $stmt->execute();
  7.  


Ten post edytował woxala123 4.02.2017, 10:39:35
Go to the top of the page
+Quote Post
viking
post
Post #16





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Bindowałeś do zapytania które nie było przygotowane na to. Innymi słowy, podstawiałeś zmienne POST bezpośrednio do zapytania a niżej jakiś zbędny kod. Już nie pamietam - PDO nie rzucało tam wyjątku że liczba parametrów zbindowanych nie równa się liczbie placeholderów?


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #17





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Właśnie nic nie wyrzuca. Działa dobrze. Przecież wyżej były trzy zmienne
Go to the top of the page
+Quote Post
viking
post
Post #18





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Podstawione bezpośrednio do SQL. Jeśli tego nie potrafisz zrozumieć to nie wiem jak inaczej wytłumaczyć.


--------------------
Go to the top of the page
+Quote Post
woxala123
post
Post #19





Grupa: Zarejestrowani
Postów: 361
Pomógł: 12
Dołączył: 9.01.2010

Ostrzeżenie: (10%)
X----


Okey poddaje się. To proszę napisz przykład jak to powinno wyglądać.
Go to the top of the page
+Quote Post
viking
post
Post #20





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Poddaje się, powinno wyglądać jak w poście 15. W poście 12 bindujesz sobie w pustkę.


--------------------
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
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 Aktualny czas: 22.08.2025 - 08:36