simple PHP code for a sign-up form that accepts a user's name, email, and password
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// Connect to database
$conn = mysqli_connect('hostname', 'username', 'password', 'database_name');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Prepare and bind
$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $password);
// Execute the statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $stmt->error;
}
// Close statement and connection
$stmt->close();
$conn->close();
}
?>
<!-- Signup form HTML -->
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<input type="submit" name="submit" value="Sign Up">
</form>
This code first checks if the form has been submitted. If it
has, it retrieves the form data and connects to the database. The code then
prepares an SQL statement to insert the user's name, email, and password into
the users table. The statement is executed, and a success or error message is
displayed based on the result. Finally, the statement and database connection
are closed.
Note: This code is for demonstration purposes only and
should not be used in production as is. It does not include proper error
handling, input validation, or password hashing for security purposes.
Comments
Post a Comment