Advertisement
Guest User

insert-post.php

a guest
Mar 8th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. <?php
  2.  
  3. if(!isset($_POST['username'], $_POST['title'], $_POST['content'])) {
  4. header('Location: new-post.html');
  5. exit();
  6. }
  7.  
  8. $servername = "localhost";
  9. $username = "hackeruser";
  10. $password = "Aa123456";
  11. $dbname = "reuven1";
  12.  
  13. //create connection
  14. $conn = new mysqli($servername, $username, $password, $dbname);
  15. // Check connection
  16. if ($conn->connect_error) {
  17. die("connection failed: " . $conn->connect_error);
  18. }
  19.  
  20. $sql = "INSERT INTO posts (username, title, content) VALUES ('".$_POST['username']."', '".$_POST['title']."', '".$_POST['content']."')";
  21.  
  22. if ($conn->query($sql) === TRUE) {
  23. echo "New record created successfully";
  24. } else {
  25. echo "Error: ".$sql."<br>".$conn->error;
  26. }
  27.  
  28. $conn->close();
  29.  
  30. // Eventually redirect to index
  31. header('Location: index.php');
  32.  
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement