Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 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.     if(empty($name) || empty($body)) {
  22.     die('Can\'t add empty fields.');
  23. }
  24.     elseif ($query == true) {
  25.     echo 'Message submitted.';
  26. }
  27.     else {
  28.     die('Entry wasn\'t added');
  29. }
  30.  
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement