Guest User

Untitled

a guest
Dec 7th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?PHP
  2. $hostname = "websqurl.db.8248509.hostedresource.com";
  3. $database = "websqurl";
  4. $username = "websqurl";
  5. $password = "Sicm1149!";
  6. $connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
  7.  
  8. function sanitize($value, $type)
  9. {
  10.   $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;
  11.  
  12.   switch ($type) {
  13.     case "text":
  14.       $value = ($value != "") ? "'" . $value . "'" : "NULL";
  15.       break;
  16.     case "long":
  17.     case "int":
  18.       $value = ($value != "") ? intval($value) : "NULL";
  19.       break;
  20.     case "double":
  21.       $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
  22.       break;
  23.     case "date":
  24.       $value = ($value != "") ? "'" . $value . "'" : "NULL";
  25.       break;
  26.   }
  27.  
  28.   return $value;
  29. }
  30.  
  31. //include the connection file
  32.  
  33. require_once('websqurlsubmission.php');
  34.  
  35. //save the data on the DB and send the email
  36.  
  37. if(isset($_POST['action']) && $_POST['action'] == 'submitform')
  38. {
  39.     //recieve the variables
  40.  
  41.     $name = $_POST['name'];
  42.     $email = $_POST['email'];
  43.     $sitename = $_POST['sitename'];
  44.     $url = $_POST['url'];
  45.     $cat = $_POST['category'];
  46.     $subcat = $_POST['subcategory'];
  47. }
  48.  
  49. //save the data on the DB
  50.  
  51. mysql_select_db($database, $connection);
  52.  
  53. $insert_query = sprintf("INSERT INTO websqurlsubmit (name, email, sitename, url, category, subcategory) VALUES (%s, %s, %s, %s, %s, %s)",
  54.                         sanitize($name, "text"),
  55.                         sanitize($email, "text"),
  56.                         sanitize($sitename, "text"),
  57.                         sanitize($url, "text"),
  58.                         sanitize($cat, "text"),
  59.                         sanitize($subcat, "text"));
  60.  
  61. $result = mysql_query($insert_query, $connection) or die(mysql_error());
  62.  
  63. //form validation
  64.  
  65. /*
  66.  *  Specify the field names that are in the form. This is meant
  67.  *  for security so that someone can't send whatever they want
  68.  *  to the form.
  69.  */
  70. $allowedFields = array(
  71.     'name',
  72.     'email',
  73.     'sitename',
  74.     'url',
  75.     'category',
  76.     'subcategory',
  77. );
  78.  
  79. // Specify the field names that you want to require...
  80. $requiredFields = array(
  81.     'email',
  82.     'sitename',
  83.     'url',
  84.     'category',
  85. );
  86.  
  87. // Loop through the $_POST array, which comes from the form...
  88. $errors = array();
  89. foreach($_POST AS $key => $value)
  90. {
  91.     // first need to make sure this is an allowed field
  92.     if(in_array($key, $allowedFields))
  93.     {
  94.         $$key = $value;
  95.  
  96.         // is this a required field?
  97.         if(in_array($key, $requiredFields) && $value == '')
  98.         {
  99.             $errors[] = "The field $key is required.";
  100.         }
  101.     }
  102. }
  103.  
  104. // were there any errors?
  105. if(count($errors) > 0)
  106. {
  107.     $errorString = '<p>There was an error processing the form.</p>';
  108.     $errorString .= '<ul>';
  109.     foreach($errors as $error)
  110.     {
  111.         $errorString .= "<li>$error</li>";
  112.     }
  113.     $errorString .= '</ul>';
  114.  
  115.     // display the previous form
  116.     include 'index.php';
  117. }
  118. else
  119. {
  120.     // At this point you can send out an email or do whatever you want
  121.     // with the data...
  122.  
  123.     // each allowed form field name is now a php variable that you can access
  124.  
  125.     // display the thank you page
  126.     header("Location: thankyou.html");
  127. }
  128.  
  129. ?>
Add Comment
Please, Sign In to add comment