Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Początki z OOP, klasa dodająca posty do bazy danych
adolf1945
post
Post #1





Grupa: Zarejestrowani
Postów: 29
Pomógł: 0
Dołączył: 7.05.2010

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


Stworzyłem klasę dodającą post do bazy danych, lecz niestety nie działa ( nie wyświetla żadnego błędu - po prostu nie dodaje danych do bazy danych)

Nazwa tabeli: post
id INT
title VARCHAR
content TEXT


class.post.php

  1. <?php
  2. class Post
  3. {
  4. private $title;
  5. private $content;
  6.  
  7. public function __construct() {
  8. $this->title = $this->filter($_POST['xtitle']);
  9. $this->content = $this->filter($_POST['xtext']);
  10. }
  11.  
  12. public function addPost()
  13. {
  14. mysql_connect("localhost","root","") or die(mysql_error());
  15. return mysql_query("INSERT INTO post(title,content) VALUES ('{$this->title}','{$this->content}')");
  16. }
  17.  
  18. public function filter($var)
  19. {
  20. return preg_replace('/[^a-zA-Z0-9]/','',$var);
  21. }
  22.  
  23. }
  24.  
  25. ?>
  26.  


post.php

  1. <?php
  2. include_once('/class.post.php');
  3.  
  4. $post = new Post();
  5.  
  6.  
  7.  
  8. ?>
  9. <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
  10. <table>
  11. <tr><td>Title:</td><td><input type="text" name="xtitle"/></td></tr>
  12. <tr><td>Content:</td><td><input type="text" name="xtext"/></td></tr>
  13. </table>
  14. <input type="submit" name="dodaj" value="Dodaj"/>
  15. </form>
  16.  


W przyszłości chcę stworzyć metody odpowiadające za usuwanie danych i ich edycję.
Co sądzicie o budowie tej klasy?

Ten post edytował adolf1945 8.08.2011, 16:00:41
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: 19.09.2025 - 18:41