Guest User

Untitled

a guest
Dec 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <?php
  2.  
  3. include "db_connect.php";
  4.  
  5. $new_joke_question = $_GET["newjoke"];
  6. $new_joke_answer = $_GET["newanswer"];
  7.  
  8. // Search the database for the word chicken
  9. echo "<h2>Trying to add a new joke and answer: $new_joke_question
  10. $new_joke_answer </h2>";
  11.  
  12. $sql = "INSERT INTO Jokes_table (JokeID, Joke_question, Joke_answer) VALUES
  13. (NULL, '$new_joke_question', '$new_joke_answer' )";
  14. $result = $mysqli->query($sql);
  15.  
  16. include "search_all_jokes.php";
  17. ?>
  18. <a href="index.php">Return to the main page</a>
  19.  
  20. <?php
  21.  
  22. // four variables to connect the database
  23. $host = "localhost";
  24. $username = "root";
  25. $user_pass = "usbw";
  26. $database = "test";
  27.  
  28. // create a database connection instance
  29. $mysqli = new mysqli($host, $username, $user_pass, $database);
  30.  
  31. ?>
  32.  
  33. // if there are any values in the table, select them one at a time
  34. if ($mysqli->connect_errno) {
  35. echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " .
  36. $mysqli->connect_error;
  37. }
  38. echo $mysqli->host_info . "<br>";
  39. $sql = "SELECT JokeID, Joke_question, Joke_answer FROM Jokes_table";
  40. $result = $mysqli->query($sql);
  41.  
  42. if ($result->num_rows > 0) {
  43. // output data of each row
  44. while($row = $result->fetch_assoc()) {
  45. echo "JokeID: " . $row["JokeID"]. " - Joke_question: " .
  46. $row["Joke_question"]. " " . $row["Joke_answer"]. "<br>";
  47. }
  48. } else {
  49. echo "0 results";
  50. }
  51.  
  52.  
  53. ?>
  54.  
  55. <?php
  56.  
  57. include "db_connect.php";
  58.  
  59. $new_joke_question = $_GET["newjoke"];
  60. $new_joke_answer = $_GET["newanswer"];
  61.  
  62. $new_joke_question = $mysqli->real_escape_string($new_joke_question);
  63. $new_joke_answer = $mysqli->real_escape_string($new_joke_answer);
  64.  
  65. // Search the database for the word chicken
  66. echo "<h2>Trying to add a new joke and answer: $new_joke_question $new_joke_answer
  67. </h2>";
  68.  
  69. if ($mysqli->connect_errno) {
  70. echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  71. }
  72. echo $mysqli->host_info . "<br>";
  73. $sql = "INSERT INTO Jokes_table (JokeID, Joke_question, Joke_answer) VALUES (' ',
  74. '$new_joke_question', '$new_joke_answer' )";
  75. $result = $mysqli->query($sql);
  76.  
  77.  
  78. if ($mysqli->query($sql) === TRUE) {
  79. echo 'users entry saved successfully';
  80. }
  81. else {
  82. echo 'Error: '. $mysqli->error .'<br>';
  83. }
  84.  
  85.  
  86. include "search_all_jokes.php";
  87. ?>
  88. <a href="index.php">Return to the main page</a>
Add Comment
Please, Sign In to add comment