Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php // Create a database connection.
  2. $dbhost = "localhost";
  3. $dbuser = "blogtest";
  4. $dbpass = "Kapaldo123";
  5. $dbname = "blog";
  6. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  7.  
  8. //Test if connection occurred.
  9. if (mysqli_connect_errno()) {
  10. die("Database connection failed: " .
  11. mysqli_connect_error() .
  12. " (" . mysqli_connect_errno() . ")"
  13. );
  14. }
  15.  
  16. //Perform database query
  17. $name = $_POST['name'];
  18. $title = $_POST['title'];
  19. $body = $_POST['body'];
  20.  
  21. //This function will clean the data and add slashes.
  22. // Since I'm using the newer MySQL v. 5.7.14 I have to addslashes
  23. $name = mysqli_real_escape_string($connection, $name);
  24. $title = mysqli_real_escape_string($connection, $title);
  25. $body = mysqli_real_escape_string($connection, $body);
  26.  
  27. //This should retrive HTML form data and insert into database
  28. $query  = "INSERT INTO posts (name, title, body)
  29.            VALUES ('".$_POST["name"]."','".$_POST["title"]."','".$_POST["body"]."')";
  30.  
  31.         $result = mysqli_query($connection, $query);
  32.         //Test if there was a query error
  33.         if ($result) {
  34.             //SUCCESS
  35.         header('Location: activity.php');
  36.         } else {
  37.             //FAILURE
  38.             die("Database query failed. " . mysqli_error($connection));
  39.             //last bit is for me, delete when done
  40.         }
  41.  
  42. //mysqli_close($connection);
  43. ?>
  44.  
  45.  
  46. <?php
  47.  
  48.         //This will fetch the data from the database
  49.         $query = "SELECT * FROM reviews";
  50.         $result = mysqli_query($connection, $query);
  51.         //Test if there was a query error
  52.         if (!$result) {
  53.             die("Database query failed.");
  54.         }
  55.  
  56.         // This will let me display the data.
  57.         // The loop will be spilt so I can format with HTML
  58.         while ($row = mysqli_fetch_assoc($result)) {
  59.             //output data from each row
  60. ?>
  61.  
  62.         Name: <?php echo $row["name"] . "<br />"; ?>
  63.         Title: <?php echo $row["title"] . "<br />"; ?>
  64.         Review: <?php echo $row["body"] . "<br />"; ?>
  65.             echo "<hr>";
  66.   <?php
  67.         } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement