Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.  
  3. $hostname = 'localhost';
  4. $username = 'codinglo_dev';
  5. $password = 'dev123';
  6.  
  7. $database = 'codinglo_dev';
  8.  
  9. $con = mysql_connect($hostname, $username, $password) or die(mysql_error()); // I would avoid using or dies for MySQL, but that's just me.
  10.  
  11. mysql_select_db($database) or die(mysql_error());
  12.  
  13. // We need to sanitize the user input.
  14. // First mysql_real_escape_string makes sure nothing "unexpected" is going in MySQL.
  15. // htmlspecialchars removes any HTML formatting that is not needed.
  16. $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
  17. $body = mysql_real_escape_string(htmlspecialchars($_POST['body']));
  18.  
  19. $query = mysql_query("INSERT INTO blog (name, body) VALUES('$name', '$body')");
  20.  
  21.  
  22.     if(!empty($name) || ($body)) {
  23.     echo 'Message submitted.';
  24. }
  25. elseif ($query == true) {
  26.     echo 'Message submitted.';
  27. }
  28.     else {
  29. die('Can\'t add empty fields.');
  30. }
  31.  
  32.     if(empty($name) || ($body)) {
  33.     die('Can\'t add empty fields.');
  34. }
  35.     elseif ($query == true) {
  36.     echo 'Message submitted.';
  37. }
  38.     else {
  39.     die('Entry wasn\'t added');
  40. }
  41.  
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement