Post data to DB using PHP


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // Define incoming data
  4. $atm = $_REQUEST['atm'];
  5. $access = $_REQUEST['access'];
  6. $loan = $_REQUEST['loan'];
  7. $checking = $_REQUEST['checking'];
  8. $online = $_REQUEST['online'];
  9. $advantage = $_REQUEST['advantage'];
  10. $contact_name = $_REQUEST['contact_name'];
  11. $contact_email = $_REQUEST['contact_email'];
  12. $contact_zip = $_REQUEST['contact_zip'];
  13.  
  14. // Validate Data
  15. if(!$atm || !$access || !$loan || !$checking || !$online || !$advantage || !$contact_name || !$contact_email || !$contact_zip){
  16. ob_end_clean();
  17. header("Location: http://www.anywhere_you_want.com/error");
  18. exit();
  19. }else{
  20.  
  21. // Define database connection
  22. $db_host = 'localhost';
  23. $db_name = 'DB NAME';
  24. $db_user = 'YOUR USER NAME';
  25. $db_pass = 'YOUR PASSWORD';
  26. $db_table = 'DB TABLE';
  27.  
  28. // Connect to database
  29. $link = mysql_connect($db_host, $db_user, $db_pass);
  30. $db = mysql_select_db($db_name);
  31.  
  32. // Generate a sql statement
  33. $sql = "INSERT INTO $db_table (id, date_saved, atm, access, loan, checking, online, advantage, contact_name, contact_email, contact_zip) VALUES ('', NOW(), ";
  34. $sql .= "'" . mysql_real_escape_string($atm) . "', ";
  35. $sql .= "'" . mysql_real_escape_string($access) . "', ";
  36. $sql .= "'" . mysql_real_escape_string($loan) . "', ";
  37. $sql .= "'" . mysql_real_escape_string($checking) . "', ";
  38. $sql .= "'" . mysql_real_escape_string($online) . "', ";
  39. $sql .= "'" . mysql_real_escape_string($advantage) . "', ";
  40. $sql .= "'" . mysql_real_escape_string($contact_name) . "', ";
  41. $sql .= "'" . mysql_real_escape_string($contact_email) . "', ";
  42. $sql .= "'" . mysql_real_escape_string($contact_zip) . "'";
  43. $sql .= ")";
  44.  
  45. // Execute the statement
  46. $result = mysql_query($sql, $link);
  47.  
  48. // Now send an email to the admin
  49. $email_recipient = 'YOUR EMAIL HERE';
  50. $email_subject = 'Quiz Response from ' . $contact_name;
  51. $email_body = 'You received a quiz response on ' . date('m/d/Y') . ' at ' . date('H:i:s') . "n";
  52. $email_body .= "------------------------------------------------------" . "n";
  53. $email_body .= "Contact Name : " . $contact_name . "n";
  54. $email_body .= "Contact Email : " . $contact_email . "n";
  55. $email_body .= "Contact Zip : " . $contact_zip . "n";
  56. $email_body .= "------------------------------------------------------" . "n";
  57. $email_body .= "ATM Response : " . $atm;
  58. if($atm == "75,000"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  59. $email_body .= "n";
  60. $email_body .= "Access Response : " . $access;
  61. if($access == "All of the above"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  62. $email_body .= "n";
  63. $email_body .= "Loan Response : " . $loan;
  64. if($loan == "All of the above"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  65. $email_body .= "n";
  66. $email_body .= "Checking Response : " . $checking;
  67. if($checking == "Yes"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  68. $email_body .= "n";
  69. $email_body .= "Online Response : " . $online;
  70. if($online == "No"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  71. $email_body .= "n";
  72. $email_body .= "Advantage Response : " . $advantage;
  73. if($advantage == "All of the above"){$email_body .= " - Correct";} else {$email_body .= " - Incorrect";}
  74. $email_body .= "n";
  75. $email_body .= "------------------------------------------------------" . "n";
  76.  
  77. // SEND THE EMAIL
  78. mail($email_recipient, $email_subject, $email_body);
  79. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.