I'm making a relatively easy login and register form to practice my web skills and I'm having issues with the PHP/SQL portion of the site I have a very simple form with email password username and full name options but whenever I fill in the form and click submit the query will be send with the posted data but it will not be added to the database.
What does work if for example i have my if(isset($POST["submitbutton"]))
and I put an !
in front of the isset
the data WILL be sent into the database with no errors so this led me to believe something is wrong with my POST or submit button
P.S: i have also tried using print_r
and var_dump
. Both gave me results such as this if i put var_dump
BEFORE the if(isset)
i get this, array(4) { ["reg_username"]=> string(3) "www" ["reg_password"]=> string(3) "waw" ["reg_email"]=> string(16) "test@hotmail.com" ["reg_fullname"]=> string(3) "hhh" }
If I put it after the POST nothing will be shown anymore
This is my database connection file
<?php
$host ="localhost";
$user = "root";
$password ="";
$dbname="registerlogin";
$conn = mysqli_connect($host,$user,$password,$dbname) or die ($conn -> error);
$sql = "SELECT * FROM `users`";
$sqlQuery = mysqli_query($conn,$sql);
$query = array();
while($row = mysqli_fetch_assoc($sqlQuery)){
$query = $row;
}
?>
this is the register PHP file
<?php
include_once("db.php");
if(isset($_POST["registerbutton"])){
$username = mysqli_real_escape_string($conn,$_POST["reg_username"]);
$hasedpassword = password_hash($_POST["reg_password"],PASSWORD_BCRYPT);
$email = mysqli_real_escape_string($conn,$_POST["reg_email"]);
$fullname = mysqli_real_escape_string($conn,$_POST["reg_fullname"]);
$sqlInsert = "INSERT INTO `users` (`User-id`,`Username`,`Password`,`Email`,`Fullname`) VALUES (NULL,'$username','$hasedpassword','$email','$fullname')";
if(!$conn){
die("Error: ".mysql_error());
}
if (count($_POST) > 0){
mysqli_query($conn,$sqlInsert) or die($conn-> error);
$message = "Successfully registerd";
} else{
$message = "Something went wrong";
}
}
And the HTML file
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="register-form" class="text-left">
<div class="login-form-main-message"></div>
<div class="main-login-form">
<div class="login-group">
<div class="form-group">
<label for="reg_username" class="sr-only">Email address</label>
<input type="text" class="form-control" id="reg_username" name="reg_username" placeholder="username">
</div>
<div class="form-group">
<label for="reg_password" class="sr-only">Password</label>
<input type="password" class="form-control" id="reg_password" name="reg_password" placeholder="password">
</div>
<div class="form-group">
<label for="reg_email" class="sr-only">Email</label>
<input type="text" class="form-control" id="reg_email" name="reg_email" placeholder="email">
</div>
<div class="form-group">
<label for="reg_fullname" class="sr-only">Full Name</label>
<input type="text" class="form-control" id="reg_fullname" name="reg_fullname" placeholder="full name">
</div>
</div>
<button type="submit" value="Submit form" name="registerbutton"><i class="fa fa-chevron-right"></i></button>
</div>
<div class="etc-login-form">
<p>already have an account? <a href="#">login here</a></p>
</div>
</form>