Untitled
unknown
plain_text
2 years ago
4.1 kB
20
Indexable
<?php
// Database connection parameters
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Define variables to store user input and error messages
$displayName = $username = $password = $retypePassword = "";
$displayNameErr = $usernameErr = $passwordErr = $retypePasswordErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Function to sanitize and validate input data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Validate display name
if (empty($_POST["displayName"])) {
$displayNameErr = "Display Name is required";
} else {
$displayName = test_input($_POST["displayName"]);
}
// Validate username
if (empty($_POST["username"])) {
$usernameErr = "Username is required";
} else {
$username = test_input($_POST["username"]);
}
// Validate password
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} elseif (strlen($_POST["password"]) < 6) {
$passwordErr = "Password must be at least 6 characters";
} else {
$password = test_input($_POST["password"]);
}
// Validate retype password
if (empty($_POST["retypePassword"])) {
$retypePasswordErr = "Retype Password is required";
} elseif ($_POST["password"] != $_POST["retypePassword"]) {
$retypePasswordErr = "Passwords do not match";
} else {
$retypePassword = test_input($_POST["retypePassword"]);
}
// If there are no errors, save the data to the database
if (empty($displayNameErr) && empty($usernameErr) && empty($passwordErr) && empty($retypePasswordErr)) {
// Hash the password before saving it to the database
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Prepare and execute an SQL statement to insert the data into the database
$stmt = $conn->prepare("INSERT INTO users (display_name, username, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $displayName, $username, $hashedPassword);
if ($stmt->execute()) {
echo "Registration successful! Account saved to the database.";
// Reset the form fields
$displayName = $username = $password = $retypePassword = "";
} else {
echo "Error: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
}
// Close the database connection
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Account Registration</title>
</head>
<body>
<h2>Account Registration</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="displayName">Display Name:</label>
<input type="text" name="displayName" value="<?php echo $displayName; ?>">
<span class="error"><?php echo $displayNameErr; ?></span>
<br><br>
<label for="username">Username:</label>
<input type="text" name="username" value="<?php echo $username; ?>">
<span class="error"><?php echo $usernameErr; ?></span>
<br><br>
<label for="password">Password:</label>
<input type="password" name="password">
<span class="error"><?php echo $passwordErr; ?></span>
<br><br>
<label for="retypePassword">Retype Password:</label>
<input type="password" name="retypePassword">
<span class="error"><?php echo $retypePasswordErr; ?></span>
<br><br>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
Editor is loading...
Leave a Comment