Advertisement
vvccs

wt_13_insertdata

Oct 13th, 2024 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.44 KB | None | 0 0
  1. form.html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>User Registration</title>
  8. </head>
  9. <body>
  10. <h2>User Registration Form</h2>
  11. <form action="insert.php" method="POST">
  12. <label for="username">Username:</label>
  13. <input type="text" id="username" name="username" required>
  14. <br><br>
  15. <label for="email">Email:</label>
  16. <input type="email" id="email" name="email" required>
  17. <br><br>
  18. <input type="submit" value="Submit">
  19. </form>
  20. </body>
  21. </html>
  22.  
  23.  
  24. insert.php
  25. <?php
  26. // Database configuration
  27. $host = 'localhost'; // Change if your DB is hosted elsewhere
  28. $dbname = 'WT';
  29. $username = 'root'; // Default username for MySQL
  30. $password = ''; // Default password (leave empty if none)
  31. // Create a connection
  32. $conn = new mysqli($host, $username, $password, $dbname);
  33. // Check the connection
  34. if ($conn->connect_error) {
  35. die("Connection failed: " . $conn->connect_error);
  36. }
  37. // Check if the form is submitted
  38. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  39. // Get user input
  40. $user = $conn->real_escape_string($_POST['username']);
  41. $email = $conn->real_escape_string($_POST['email']);
  42. // Insert query
  43. $sql = "INSERT INTO users (username, email) VALUES ('$user', '$email')";
  44.  
  45. if ($conn->query($sql) === TRUE) {
  46. echo "New record created successfully";
  47. } else {
  48. echo "Error: " . $sql . "<br>" . $conn->error;
  49. }
  50. }
  51. // Close the connection
  52. $conn->close();
  53. ?>
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement