Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP] Problem z zapytaniem do bazy
Forum PHP.pl > Forum > Przedszkole
necromantiq
Siema, napisałem taką oto klasę do insertowania danych do db:

  1. <?php
  2.  
  3. class addNews {
  4.  
  5. public $title;
  6. public $content;
  7. public $author;
  8.  
  9. public function addTitle($title){
  10.  
  11. $this->title = $title;
  12.  
  13. }
  14.  
  15. public function addContent($content){
  16.  
  17. $this->content = $content;
  18.  
  19. }
  20.  
  21. public function addAuthor($author){
  22.  
  23. $this->author = $author;
  24.  
  25. }
  26.  
  27. public function save(){
  28.  
  29. try{
  30.  
  31. require_once('pdo.php');
  32.  
  33. $ins = $pdo->exec('INSERT INTO `news` (`title`, `content`, `author`) VALUES ('$this->title', '$this->content', '$this->author')');
  34. }
  35. catch(PDOExeptions $e){
  36.  
  37. echo 'Wystąpił błąd biblioteki PDO: ' . $e->getMessage();
  38.  
  39. }
  40. }
  41. }
  42. ?>


Lecz non stop wywala mi błąd, Parse error: syntax error, unexpected T_VARIABLE in D:\Projekty\vs\class.addnews.php on line 33 , próbowałem już na różne sposoby i jedynym sukcesem było insertowanie danych jako całego stringa $this->title, $this->content, $this->author. Wiecie może gdzie znajduje się problem??
skowron-line
Czegoś Ci brakuje w tym stringu co go do exec przekazujesz
  1. $ins = $pdo->exec('INSERT INTO `news` (`title`, `content`, `author`) VALUES ('. $this->title .', '. $this->content .', '. $this->author .')');

poczytaj o łączeniu stringów.
necromantiq
niestety teraz wywala mi inne błędy:

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 'news, tresc newsa, autor newsa)' at line 1' in D:\Projekty\vs\class.addnews.php on line 33

i drugi błąd:

PDOException: 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 'news, tresc newsa, autor newsa)' at line 1 in D:\Projekty\vs\class.addnews.php on line 33
amii
  1. $ins = $pdo->exec("INSERT INTO `news` (`title`, `content`, `author`) VALUES ($this->title, $this->content, $this->author)");

?
necromantiq
no niestety ;/
lobopol
  1. $ins = $pdo->exec('INSERT INTO `news` (`title`, `content`, `author`) VALUES ("'. $this->title .'", "'. $this->content .'", "'. $this->author .'")');

necromantiq
dzieki lobopol smile.gif teraz wszystko śmiga smile.gif
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.