Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. //form.php
  2. <html>
  3. <body>
  4.  
  5. <form action="send.php" method="post">
  6. First name: <input type="text" name="firstName"><br><br>
  7. Last name: <input type="text" name="lastName"><br><br>
  8. E-mail: <input type="text" name="email"><br><br>
  9. <input type="submit">
  10. </form>
  11.  
  12. </body>
  13. </html>
  14.  
  15. //send.php
  16. <?php
  17. //Put login info into variables for security reasons
  18. $host = "localhost";
  19. $username = "root";
  20. $password = "";
  21. $dbname = "form";
  22.  
  23. // Create connection
  24. $conn = new mysqli($host, $username, $password, $dbname);
  25.  
  26. // Check connection
  27. if ($conn->connect_error) {
  28. die("Connection failed: " . $conn->connect_error);
  29. }
  30.  
  31. // escape variables for security
  32. $firstName = mysqli_real_escape_string($conn, $_POST['firstName']);
  33. $lastName = mysqli_real_escape_string($conn, $_POST['lastName']);
  34. $email = mysqli_real_escape_string($conn, $_POST['email']);
  35.  
  36. // Insert data in SQL Table
  37. $sql = "INSERT INTO form (firstName, lastName, email)
  38.  
  39. VALUES ('$firstName', '$lastName', '$email')";
  40.  
  41. // Report if data transfer successful or error
  42. if ($conn->query($sql) === TRUE) {
  43. echo "New record created successfully";
  44. } else {
  45. echo "Error: " . $sql . "<br>" . $conn->error;
  46. }
  47.  
  48. // Close connection to database
  49. $conn->close();
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement