Sure! Here's an example of a simple signup form using PHP and MySQL database.
![]() |
Signup form using PHP and MySQL Database |
Create a database table
First, create a database table to store user information. Here's an example:
sql
Copy code
CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL
);
Create a form
Create a simple HTML form to collect user information, including username, email, and password.
php
Copy code
<form method="post" action="signup.php">
<input type="text" name="username" placeholder="Username">
<br>
<input type="email" name="email" placeholder="Email">
<br>
<input type="password" name="password" placeholder="Password">
<br>
<input type="submit" name="submit" value="Sign Up">
</form>
Process form data
Create a PHP script to process the form data and insert it into the database.
php
Copy code
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check for form submission
if (isset($_POST['submit'])) {
// Get form data
$username = mysqli_real_escape_string($conn, $_POST['username']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
// Hash the password
$password = password_hash($password, PASSWORD_DEFAULT);
// Insert user data into database
$query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
mysqli_query($conn, $query);
// Redirect to success page
header("Location: success.php");
exit();
}
?>
Display success message
Create a success page to display a message to the user after they have successfully signed up.
css
Copy code
<h1>Success!</h1>
<p>Thank you for signing up!</p>
And that's it! This is a simple example of a signup form using PHP and MySQL database. Of course, there are many ways to improve this code and add more features, but this should be a good starting point.
Post a Comment