-1

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>';
    }
}

}

?>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Josh
  • 31
  • 5
  • 1
    Possible duplicate of [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – esqew Aug 23 '18 at 19:16
  • It's not the error message that's not working. There is something in my code that's making that error message appear. – Josh Aug 23 '18 at 19:23
  • Based on what you've written above, it's my understanding that the error reporting *is* what's not working, considering a 500 error thrown by the server is not very descriptive to what is actually causing said error. Employing some of the strategies described in the linked question will help you formulate a more concise question, to which you'll most likely receive more valuable answers to. – esqew Aug 23 '18 at 19:25
  • Was this question solved from the answer given below? @Josh If so, it'd be nice to see it marked as such by accepting the answer. – Funk Forty Niner Aug 26 '18 at 01:18

1 Answers1

2

your conditions are not properly closed, there is no "if" for this "else"

else {
        echo '<div class="alert alert-warning">That Email Already Exist.</div>';
    }
}

You should look for a PHP editor that helps you to format the code properly and find inconsistences (MS Code maybe? or Brackets?).

Alex Angelico
  • 3,710
  • 8
  • 31
  • 49