trying to insert values into my table for first time
I am getting to the success branch of my code with no errors appearing on the php page or in my log. I am not getting a user with password inserted into my users table. I can insert them using the workbench but not using PHP.
<?php
if (isset($_POST['submit'])) {
// add databse connection
require 'database.php';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmPass = $_POST['confirmpassword'];
if (empty($username) || empty($password) || empty($confirmPass)) {
header ("location: ../register.php?error=emptyfields&username=" . $username);
exit();
} elseif (!preg_match("/^[a-zA-Z0-9]*/", $username)) {
header ("location: ../register.php?error=Invalidusername&username=" . $username);
exit();
} elseif ($password !== $confirmPass) {
header ("location: ../register.php?error=PasswordsDoNotMatch&username=" . $username);
exit();
}
else {
$sql = "SELECT name FROM users WHERE name = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header ("location: ../register.php?error=SQLerror(line26)" . $username);
exit();
} else {
//passing a strint. 2strings is ss, boolean, integer
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
//next line is to fetch
mysqli_stmt_store_result($stmt);
//lookinmg for 0 unique or 1 already exists
$rowcount = mysqli_stmt_num_rows($stmt);
if ($rowcount > 0) {
header ("location: ../register.php?error=UsernameAlreadyExists:" . $username);
exit();
//placeholders into values for prepared statements
} else {
$sql = "INSERT INTO users (name, password) VALUES (? , ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header ("location: ../register.php?error=SQLerror" . $username);
exit();
} else {
$hashedPass = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ss", $username, $hashedPass);
mysqli_stmt_execute($stmt);
header ("location: ../register.php?success=Registered:" . $username);
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
?>
Subject
Written By
Posted
trying to insert values into my table for first time
March 18, 2021 09:40PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.