I hope someone can find the mistake I made. I followed a tutorial on youtube because I am new to php. I have a modal, a pop up form where the user can register and the data suppose to be transferred to MySql database on my XAMPP server. Unfortunately no data is transferred to my database.
Here is some html/css code:
<form action="includes/signup.inc.php" method="post" class="form-box">
<div class="input-box">
<label class="label-text">Username</label>
<div class="form-group">
<span class="la la-user form-icon"></span>
<input class="form-control form-control-styled" type="text" id="username" placeholder="Username">
</div>......
<button id="registeracc" type="submit" name="registeracc" class="theme-btn gradient-btn w-100">
<i class="la la-user-plus mr-1"></i> Register Account
</button>
Code to connect to mysqli database (filename: dbh.php):
<?php
$serverName = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "knot-x";
$conn = mysqli_connect ($serverName, $dbUsername, $dbPassword, $dbName);
if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}
?>
Code for signup in my signup.inc.php file with checking if the form is filled out:
<?php
if (isset($_POST['registeracc'])) {
$username = $_POST["username"];
$email = $_POST["email"];
$pwd = $_POST["pwd"];
require_once 'dbh.php';
require_once 'functions.inc.php';
// Function for empty signup form
if (emptyInputSignup($username, $email, $pwd) !== false) {
header("location: ../index.php?error=emptyinput");
exit();
}
if (invalidUsername($username) !== false) {
header("location: ../index.php?error=invalidusername");
exit();
}
if (invalidEmail($email) !== false) {
header("location: ../index.php?error=invalidemail");
exit();
}
if (usernameExists($conn, $username, $email) !== false) {
header("location: ../index.php?error=usernametaken");
exit();
}
createUser($conn, $username, $email, $pwd);
}
else {
header("location: ../index.php");
exit();
}
?>
Code from my functions.inc.php file for the function to show error if the user register with empty form:
<?php
function emptyInputSignup($username, $email, $pwd) {
$result;
if (empty($username) || empty($email) || empty($pwd)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function invalidUsername($username){
$result;
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function invalidEmail($email){
$result;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function usernameExists($conn, $username, $email){
$sql = "SELECT * FROM users WHERE userName = ? OR userEmail = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../index.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
}
else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function createUser($conn, $username, $email, $pwd){
$sql = "INSERT INTO users (userName, userEmail, userPwd) VALUES (?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../index.php?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../index.php?error=none");
exit();
}
?>
In my url I get the error message:
localhost/.../index.php?error=emptyinput