Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][PDO] Zabezpieczanie zapytań
Kerth
post 1.07.2014, 18:22:55
Post #1





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Witajcie smile.gif

Podobno, każde zapytania powinno się zabezpieczać za pomocą bindparam() czy jakoś tak. Może mi podać ktoś przykład takiego zapytania? Np na moim kodzie:

  1. $register_zadania = $db->exec("INSERT INTO `zadania` (`nick`, `ip`, `data`, `nazwa` ) VALUES ('$nick', '$ip', '$data', '$nazwa' )") or die(mysql_error());
Go to the top of the page
+Quote Post
Crozin
post 1.07.2014, 18:44:45
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Wiesz, że dane należy podpinać przez bindValue/bindParam, więc dlaczego nie sprawdzisz bezpośrednio w dokumentacji: http://www.php.net/manual/en/pdostatement.bindvalue.php
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 18:58:24
Post #3





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


  1. $register = $db->exec("INSERT INTO `gracze` (`nick`, `ip`, `email`, `haslo`, `haslo2`, `data`, `nazwa` ) VALUES (?,?,?,?,?,?,?)") or die(mysql_error());
  2.  
  3. $register->bindValue(1, $nick, PDO::PARAM_STR);
  4. $register->bindValue(2, $ip, PDO::PARAM_INT);
  5. $register->bindValue(3, $email, PDO::PARAM_STR);
  6. $register->bindValue(4, $haslo, PDO::PARAM_STR);
  7. $register->bindValue(5, $haslo2, PDO::PARAM_STR);
  8. $register->bindValue(6, $data, PDO::PARAM_INT);
  9. $register->bindValue(7, $nazwa, PDO::PARAM_STR);


Skorzystałem z innego zapytania. Może mi ktoś powiedzieć co jest nie tak? Bo niestety nie działa mi ten kod.
Go to the top of the page
+Quote Post
Turson
post 1.07.2014, 19:00:31
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Spójrz jeszcze raz na przykład w manualu i porównaj ze swoim...
Go to the top of the page
+Quote Post
PawelC
post 1.07.2014, 19:08:13
Post #5





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


  1. $result = $this->db->prepare("insert into category values(NULL, :name)");
  2. $result->bindParam(':name', $name, PDO::PARAM_STR);

U mnie np tak to wygląda smile.gif W manualu masz bardzo dobre przykłady co i jak zrobić smile.gif To co zrobiłeś to masło maślane.
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 19:17:48
Post #6





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Mam teraz tak:

  1. $register = $db->prepare("INSERT INTO `gracze` (`nick`, `ip`, `email`, `haslo`, `haslo2`, `data`, `nazwa` ) VALUES(nick, :ip, :email, :haslo, :haslo2, :data, :nazwa )") or die(mysql_error());
  2. $register->bindValue(":nick", $nick, PDO::PARAM_STR);
  3. $register->bindValue(":ip", $ip, PDO::PARAM_STR);
  4. $register->bindValue(":email", $email, PDO::PARAM_STR);
  5. $register->bindValue(":haslo", $haslo, PDO::PARAM_STR);
  6. $register->bindValue(":haslo2", $haslo2, PDO::PARAM_STR);
  7. $register->bindValue(":data", $data, PDO::PARAM_INT);
  8. $register->bindValue(":nazwa", $nazwa, PDO::PARAM_STR);
  9. $register->execute();



Powoduje to:
  1. Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\WebServ\httpd\strona4\register.php on line 205


Korzystam z tego: http://turson.pl/blog/php-mysql-prosty-skr...do/#comment-766

Oraz możecie mi powiedzieć czy dobrze przepisałem 3 argument bindValue dla każdego z pól? Czy data ma mieć PDO::PARAM_INT itd?
Go to the top of the page
+Quote Post
PawelC
post 1.07.2014, 19:20:34
Post #7





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


  1. VALUES(nick,

Błąd

Do podglądu zobacz w pełni działajacy kod:
  1. public function add($name)
  2. {
  3. $result = $this->db->prepare("insert into category values(NULL, :name)");
  4. $result->bindParam(':name', $name, PDO::PARAM_STR);
  5.  
  6. if(!$result->execute()){
  7. throw new Exeption("Wystąpił błąd podczas dodawania kategorii");
  8. }
  9. }


Ten post edytował ExPlOiT 1.07.2014, 19:22:16
Go to the top of the page
+Quote Post
Turson
post 1.07.2014, 19:24:22
Post #8





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Datę jako date/datetime traktuj jako string, dla timestamp to będzie int
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 19:25:00
Post #9





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


A to NULL? to jest z pola np. `id` AUTO_INCREMENT?
Go to the top of the page
+Quote Post
Turson
post 1.07.2014, 19:26:16
Post #10





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Jeżeli w INSERT gracze(kolumny) nie pomijasz id, to null będzie dla auto increment
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 19:35:41
Post #11





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Aha to rozumiem. A jak jest z zapytaniami typu UPDATE oraz SELECT?

UPDATE:

  1. $kasa_start = 1000;
  2. $register_kasa_start = $db->prepare("UPDATE `kasa` FROM `gracze` = :kasa_start WHERE `Nick` = :nick") or die(mysql_error());
  3. $register_kasa_start->bindValue(":nick", $nick, PDO::PARAM_STR);
  4. $register_kasa_start->bindValue(":kasa_start", $kasa_start, PDO::PARAM_INT);
  5. $register_kasa_start->execute();



Czy robi się to tak czy jakoś inaczej?

Ten post edytował Kerth 1.07.2014, 19:36:55
Go to the top of the page
+Quote Post
PawelC
post 1.07.2014, 19:36:27
Post #12





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


Jeżeli jest update to wpisujesz tylko te kolumny które aktualizujesz + warunek where.

Masz złe zapytanie, jest update nazwa tabeli set kolumny, czyli
  1. UPDATE `gracze` SET `kasa` = :kasa_start WHERE `Nick` = :nick") or die(mysql_error());


Ten post edytował ExPlOiT 1.07.2014, 19:37:58
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 19:37:54
Post #13





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Ale gdy chcę tak jak mam powyżej, że chcę dodać po 1000 dla każdego zarejestrowanego?
Go to the top of the page
+Quote Post
PawelC
post 1.07.2014, 19:39:00
Post #14





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


Wtedy wpisujesz bez warunku where smile.gif
Czyli:
  1. UPDATE `gracze` SET `kasa`= :kasa
Go to the top of the page
+Quote Post
Kerth
post 1.07.2014, 19:46:22
Post #15





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Wiem, że może trochę te pytania nudne jak flaki z olejem ale dlaczego to mi nic nie dodaje?

  1. $kasa_start = 1000;
  2. $register_kasa_start = $db->prepare("UPDATE `gracze` SET `kasa`= :kasa_start") or die(mysql_error());
  3. $register_kasa_start->bindValue(":kasa_start", $kasa_start, PDO::PARAM_INT);
  4. $register_kasa_start->execute();
Go to the top of the page
+Quote Post
PawelC
post 1.07.2014, 19:48:53
Post #16





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


Do wglądu masz kolejny kod, zobacz różnice smile.gif
  1. $results=$this->db->prepare("update users set pass=:pass where mail=:mail");
  2. $results->bindParam(":pass", $pass, PDO::PARAM_STR);
  3. $results->bindParam(":mail", $mail, PDO::PARAM_STR);
  4.  
  5. if(!$results->execute()){
  6. throw new Exception("Wystąpił błąd podczas resetowania hasła.");
  7. }


Ten post edytował ExPlOiT 1.07.2014, 19:49:29
Go to the top of the page
+Quote Post
ZaXaZ
post 1.07.2014, 20:04:50
Post #17





Grupa: Zarejestrowani
Postów: 285
Pomógł: 18
Dołączył: 30.01.2014
Skąd: <?=$_GET['city']?>

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


Pierwszy raz widzę żeby ktoś robił or die w pdo do tego mieszając z spl ;d, zajrzyj może do wiki tam dość dobrze wytłumaczone pdo...
  1. $kasa_start = 1000;
  2. $register_kasa_start = $db->prepare('UPDATE `gracze` SET `kasa`= :kasa_start');
  3. $register_kasa_start->bindValue(':kasa_start', $kasa_start, PDO::PARAM_INT);
  4. $register_kasa_start->execute();


poczytaj np. o metodzie errorInfo() w manualu.

Ten post edytował ZaXaZ 1.07.2014, 20:19:51


--------------------
Go to the top of the page
+Quote Post
ber32
post 2.07.2014, 09:44:09
Post #18





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

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


Tu masz link do książki, która opisuje te i inne działania na pdo i jeszcze parę innych żeczy. Książka tania 12,50zł "pdf" polecam.

http://ebookpoint.pl/ksiazki/sto-pytan-i-o...00.htm#format/e


--------------------
Go to the top of the page
+Quote Post
Turson
post 2.07.2014, 09:52:15
Post #19





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Jest też darmowa "książka" - http://pl.wikibooks.org/wiki/PHP/Biblioteka_PDO
Nie wiem jak można by płacić za książkę do PDO, gdzie nawet najbardziej nieogarnięta osoba, która miała doświadczenie z mysql_ ogarnie to do godziny

Ten post edytował Turson 2.07.2014, 09:55:16
Go to the top of the page
+Quote Post
ber32
post 2.07.2014, 10:05:26
Post #20





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

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


Turson sprawdź o której Kerth napisał pierwszego posta, a o której jeszcze pytał dalej, to trochę więcej jak godzina i przypuszczam że już wcześniej miał doczynienia z mysql_


--------------------
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: 28.04.2024 - 10:20