I am trying to make a register form for my website using PHP. Everything in the HTML code & most of the PHP code works fine, however, when I add the code for putting the data into the database the website does not work, instead, I get an error saying, 'HTTP ERROR 500'. What is wrong with my code that is making this happen?
Thanks in advance for your help.
My HTML Code:
<form role="form" method="post" id="msgform" action="phpcodes/register.php" autocomplete="off">
<fieldset>
<div class="row" style="width: 95%; margin: 0% 2.5%;">
<div class="form-group">
<input class="form-control" placeholder="First Name" value="" name="firstname" type="text" autofocus required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Last Name" name="lastname" type="text" value="" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Email" name="email" type="email" value="" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Confirm Password" name="cpassword" type="password" value="" required>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="TERMS & CONDITIONS & PRIVACY POLICY" unchecked required />
<a style="text-align: right; color: black; text-decoration: none; font-size: 11px;">I have read & accept Teen Jobs <a href="terms-and-conditions.html" style="font-size: 11px; text-decoration: none; color: #00a63f;">TERMS & CONDITIONS</a> & <a href="privacy-policy.html" style="font-size: 11px; text-decoration: none; color: #00a63f;">PRIVACY POLICY.</a></a>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="NZ WORK" unchecked required />
<a style="text-align: right; text-decoration: none; color: black; font-size: 11px;">I have the right to work in New Zealand & undertake employment.</a>
</label>
</div>
<button style="outline: none; background-color: #00a63f;" id="btn" class="btn btn-lg btn-success btn-block">REGISTER</button>
</fieldset>
</form>
My PHP Code:
<?php
session_start();
include('connection.php');
if(!$con)
{
echo "<br/>";
echo '<div class="alert alert-warning">Something went wrong, please try again!</div>';
}
else {
$fname= mysqli_real_escape_string($con,$_POST['firstname']);
$lname=mysqli_real_escape_string($con,$_POST['lastname']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$pass=mysqli_real_escape_string($con,$_POST['password']);
$cpass=mysqli_real_escape_string($con,$_POST['cpassword']);
if($cpass!=$pass) {
echo "The two passwords entered do not match!";
}
else {
$result = $con->query("SELECT * FROM `signin` WHERE `email` = '$email'");
if($result->num_rows == 0) {
$query="INSERT INTO `signin`(`user_id`, `email`, `password`) VALUES (default,'$email','$pass')";
if(mysqli_query($con,$query)) {
$last_user_id="SELECT * `user_id` FROM `signin` ORDER BY `user_id` DESC LIMIT 1";
$result = mysqli_query($con,$last_user_id);
if(mysqli_num_rows($result)>0) {
while($row=mysqli_fetch_array($result)) {
$user_id = $row['user_id'];
$_SESSION['user_id']=$user_id;
echo $user_id;
}
}
else {
echo "NOT USER ID";
}
$query1="INSERT INTO `user_prfile`(`profile_id`, `firstname`, `lastname`, `user_id`) VALUES (default,'$fname','$lname','$user_id')";
if(mysqli_query($con,$query1)) {
header('Location: ../sign-in.php');
}
else {
echo '<div class="alert alert-warning">Data not inserted</div>';
}
}
else {
echo '<div class="alert alert-warning">Data not inserted</div>';
}
}
else {
echo '<div class="alert alert-warning">Data not inserted</div>';
}
}
else {
echo '<div class="alert alert-warning">That Email Already Exist.</div>';
}
}
}
?>