I am working on a college project where I have to build a time-table management system. When I ask the user for his/her credentials for the signup system. The PHP/MySql code says that it successfully added the data into the 'students' table but when I look at the table no data has been inserted. Could someone let me know what's wrong here? Here's the code below:-
<?php
if(isset($_POST['signup'])){
require_once('connection.php');
$username = $_POST['name'];
$email = $_POST['mail'];
$phone = $_POST['num'];
$password = $_POST['pwd'];
$guard = $_POST['guard'];
$schoolName = $_POST['school-name'];
$schoolId = $_POST['school-id'];
$roll = $_POST['roll'];
$class = $_POST['class'];
$room = $_POST['room'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
header("Location: ../signup?error=invalidemail&username=".$username);
exit();
} else if(!preg_match("/^[a-zA-Z0-9]*$/", $username)){
header("Location: ../signup?error=invaliduid&email=".$email);
exit();
} else if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)){
header("Location: ../signup?error=invalidemailusername");
exit();
} else {
$sql = "SELECT full_name FROM students WHERE full_name=?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../signup?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if($resultCheck > 0){
header("Location: ../signup?error=invaliduid&email=".$email);
exit();
} else {
$sql = "INSERT INTO students (roll_no, full_name, mail_id, pwd, class_name, class_room, school_name, school_id, guardian_name, phone_no) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../signup?error=sqlerror");
exit();
} else{
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssssssssss", $roll, $username, $email, $hashedPwd, $class, $room, $schoolName, $schoolId, $guard, $phone);
mysqli_stmt_execute($stmt);
header("Location: ../index.php?signup=success");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else {
header("Location: ../signup.php");
exit();
}
?>
Just a side note, The form is step-based but I do not think that is the actual culprit behind this. Anyways here is the form below:-
<form action="PHP/signup.inc.php" method="POST" class="signup">
<div class="personal">
<div class="name">Full Name:</div>
<input type="text" id="name" name="name">
<div class="phone">Phone number:</div>
<input type="number" name="num" id="num">
<div class="mail">E-mail: </div>
<input type="email" name="mail" id="mail">
<div class="pass">Password: </div>
<input type="password" name="pwd" id="pwd">
<div class="class">Class:</div>
<input type="text" placeholder="5-A, 6-B(format)" id="cls" name="class">
</div>
<div class="category">
<div class="school-info-student">
<div class="guardians">Guardian Name:</div>
<input type="text" name="guard" id="guard">
<div class="school-name">School Name:</div>
<input type="text" name="school-name" id="schoolname1">
<div class="school-id">School-ID:</div>
<input type="text" name="school-id" id="schoolid1">
</div>
</div>
<div class="add-info">
<div class="class-info">
<div class="roll-no">Roll number:</div>
<input type="number" name="roll" id="roll">
<div class="class-num">Room-Number:</div>
<input type="text" name="room" id="room">
</div>
</div>
<div class="thank-you">
<h4 class="msg">Thank you for signing up for the web app. You will be redirected shortly.</h4>
</div>
<div class="login">
<div class="log"><a href="#">Already have an account? Log in.</a></div>
<button type="button" class="btn2">Prev</button>
<button type="button" class="btn1">Next</button>
<button type="submit" name="signup" class="btn3">Submit</button>
</div>
</form>