Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP][OOP] Nie dodaje wpisu do bazy
Turson
post
Post #1





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

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


Analizuję od godziny i nie mogę znaleźć przyczyny, dlaczego wpis nie jest dodawany do bazy, a komunikat się wyświetla.

*Struktura bazy:
wpis: `id` int(11) | `user_id` int(3) | `title` char(50) | `content` text | `data` date
*Połączenie z bazą jest.
*Zmienne post oraz z sesji poprawne
*Błędów żadnych nie wywala

Może ktoś rzuci świeżym okiem i od razu wychwyci (IMG:style_emoticons/default/smile.gif)

  1. <h3>Dodaj wpis</h3>
  2. <?PHP
  3. function form(){
  4. <div id="wpis_form">
  5. <form action="index.php?page=new" method="post">
  6. <input type="text" name="title" placeholder="Tytuł" required maxlength="30"><br>
  7. <textarea name="content" placeholder="Treść" maxlength="500" required></textarea><br>
  8. <input type="submit" name="sent" value="Dodaj wpis">
  9. </form>
  10. </div>
  11. ';
  12. }
  13. class dodajWpis extends Connect{
  14.  
  15. function Length($title,$content){
  16. if(strlen($title)>=5 && strlen($content)>=5){
  17. return true;
  18. }
  19. }
  20. function ifNotExists($title){
  21. $zap=$this->db->prepare("SELECT id FROM wpisy WHERE title=:title");
  22. $zap->bindValue(':title', $title, PDO::PARAM_STR);
  23. $zap->execute();
  24. if($zap->rowCount()==0){
  25. return true;
  26. }
  27. }
  28. function getId($title){
  29. $zap=$this->db->prepare("SELECT id FROM wpisy WHERE title=:title");
  30. $zap->bindValue(':title', $title, PDO::PARAM_STR);
  31. $zap->execute();
  32. return $zap->fetch()['id'];
  33. }
  34. function dodaj($title,$content,$user_id){
  35. $query = $this->db->prepare("INSERT INTO wpisy VALUES(null, '$user_id', :title, :content, CURDATE()");
  36. $query -> bindValue(':title', $title, PDO::PARAM_STR);
  37. $query -> bindValue(':content', $content, PDO::PARAM_STR);
  38. $query -> execute();
  39. }
  40.  
  41. }
  42. $o=new dodajWpis();
  43. if(isset($_SESSION["logged"]))
  44. {
  45. if(isset($_POST["sent"]))
  46. {
  47. $title=trim($_POST['title']);
  48. $content=trim($_POST['content']);
  49. if($o->Length($title,$content))
  50. {
  51. if($o->ifNotExists($title))
  52. {
  53. $user_id=$_SESSION["user_id"];
  54. $o->dodaj($title,$content,$user_id);
  55. echo '<a href="index.php?page=wpis&id='.$o->getId($title).'">Wpis został dodany.</a>';
  56. }
  57. else{
  58. echo "Wpis o podanym tytule już istnieje!<br>";
  59. form();
  60. }
  61. }
  62. else{
  63. echo "Tytuł i/lub treść są za krótkie!<br>";
  64. form();
  65. }
  66. }
  67. else{
  68. form();
  69. }
  70. }
  71. else{
  72. header('Location:index.php?page=login');
  73. }
  74. ?>


Ten post edytował Turson 28.10.2013, 18:12:21
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: 23.08.2025 - 04:50