Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- form.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>User Registration</title>
- </head>
- <body>
- <h2>User Registration Form</h2>
- <form action="insert.php" method="POST">
- <label for="username">Username:</label>
- <input type="text" id="username" name="username" required>
- <br><br>
- <label for="email">Email:</label>
- <input type="email" id="email" name="email" required>
- <br><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- insert.php
- <?php
- // Database configuration
- $host = 'localhost'; // Change if your DB is hosted elsewhere
- $dbname = 'WT';
- $username = 'root'; // Default username for MySQL
- $password = ''; // Default password (leave empty if none)
- // Create a connection
- $conn = new mysqli($host, $username, $password, $dbname);
- // Check the connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Check if the form is submitted
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // Get user input
- $user = $conn->real_escape_string($_POST['username']);
- $email = $conn->real_escape_string($_POST['email']);
- // Insert query
- $sql = "INSERT INTO users (username, email) VALUES ('$user', '$email')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- }
- // Close the connection
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement