i’m trying to create an account system for my personal project website. i’m having trouble with the registration; when i insert the values from the form and check the database, there is a row inserted, but it only contains empty values. PDO doesn’t throw any exceptions.
my php code:
<?php
$servername = "redacted";
$dbusername = "redacted";
$dbpassword = "redacted";
try {
$conn = new PDO("mysql:host=$servername;dbname=default", $dbusername, $dbpassword);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error: " . $e->getMessage());
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_username($_POST["username"]);
$password = password($_POST["password"]);
}
function test_username($uname) {
$uname = trim($uname);
if (!preg_match("/^[a-zA-Z0-9]*$/", $uname)) {
die("invalid character in username");
}
}
function password($password) {
$password = trim($password);
if (strlen($password) < 8 || strlen($password) > 32 || !preg_match("/^[A-Za-z0-9!@#$%^&*]*$/", $password) || !preg_match("/[A-Z]/", $password) || !preg_match("/[a-z]/", $password) || !preg_match("/[0-9]/", $password) || !preg_match("/[!@#$%^&*]/", $password)) {
die("password requirements not met");
}
$password = password_hash($password, PASSWORD_DEFAULT);
}
try {
$conn->exec("INSERT INTO users (username, password)
VALUES ('$username', '$password');");
} catch(PDOException $e) {
die("Database insert failed:" . $e->getMessage());
}
header("Location: /register-succcess.html");
?>
my form:
<form action="scripts/register.php" method="POST" onsubmit="return validateForm()" name="login">
<fieldset>
<legend>register</legend>
<div>
<label for="username">username:</label>
<input type="text" name="username" id="username" required maxlength="32">
<span id="unameError"></span>
</div>
<div>
<label for="password">password:</label>
<input type="password" name="password" id="password" required maxlength="32">
<span id="pwordError"></span>
</div>
<button type="submit">register</button>
<p><a href="login">login</a></p>
</fieldset>
</form>
inserted database row:
id, username, password, created_at
‘4’, ”, ”, ‘2024-12-09 09:24:17’
New contributor
user28694851 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.