Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. * OOP practice
  6. * @author Joey
  7. */
  8.  
  9. require_once 'config/autoload.php';
  10. require_once 'config/config.php';
  11.  
  12. // Database credentials
  13.  
  14. $host = $config['host'];
  15. $dbname = $config['dbname'];
  16. $username = $config['username'];
  17. $password = $config['password'];
  18.  
  19. // Short variable names
  20.  
  21. $fname = isset($_POST['fname']) ? clean_data($_POST['fname']) : null;
  22. $lname = isset($_POST['lname']) ? clean_data($_POST['lname']) : null;
  23. $your_question = isset($_POST['question']) ? clean_data($_POST['question']) : null;
  24.  
  25. try {
  26.  
  27. // Page object
  28.  
  29. $homepage = new Page('Questions and Answers', 'QA', '', 'You ask, we answer');
  30.  
  31. // Author object
  32.  
  33. $author = new Author($fname, $lname);
  34.  
  35. // Question object
  36.  
  37. $question = new Question($your_question, $author);
  38.  
  39. // Database object
  40.  
  41. $pdo = new DbConnect($host, $dbname, $username, $password);
  42.  
  43. // Connect to the database
  44.  
  45. $pdo->open_connection();
  46. } catch (Exception $e) {
  47. echo $e->getMessage(), '<br>';
  48. exit;
  49. } catch (PDOException $e) {
  50. echo $e->GetMessage(), '<br>';
  51. exit;
  52. }
  53.  
  54. if (isset($_POST['submit'])) {
  55. // Insert the user input into the database
  56.  
  57. $pdo->execute_query('INSERT INTO author (first_name, last_name) VALUES (?, ?)', array($author->getFirstName(), $author->GetLastName()));
  58. $pdo->close_connection();
  59. $homepage->content = '<h3>Your question</h3><br><p>Hello ' . $author->getFirstName() . ' ' . $author->getLastName() . '!' . '</p><br>';
  60. $homepage->content .= '<p>You ask: ' . $question->getQuestion() . '</p><br>';
  61. $homepage->content .= 'Do you want to ';
  62. $homepage->content .= '<a href="index.php">Ask another question?</a>' . ' ' . '<br>';
  63. } else {
  64. // Display the form
  65.  
  66. $homepage->content = <<<_END
  67.  
  68. <h3>Your question here</h3><br>
  69. <form action="{$_SERVER['PHP_SELF']}" method="post" role="form">
  70. <label for="fname">Enter your first name</label><br>
  71. <input type="text" id="fname" name="fname" size="30"><br>
  72. <label for="lname">Enter your last name</label><br>
  73. <input type="text" id="lname" name="lname" size="30"><br>
  74. <label for="question">What is your question?</label><br>
  75. <textarea id="question" name="question" rows="10" cols="70"></textarea><br>
  76. <input type="submit" name="submit" value="Submit">
  77. </form>
  78. _END;
  79. }
  80.  
  81. // Display the webpage using the Page class
  82.  
  83. $homepage->display();
  84.  
  85. function clean_data($data)
  86. {
  87. $data = trim($data);
  88. $data = strip_tags($data);
  89. $data = addslashes($data);
  90. return $data;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement