Home > Software engineering >  Error is throwing even though the form is filled in?
Error is throwing even though the form is filled in?

Time:10-22

I am creating a web app for class. The login page is working fine. I have a form with action="login.php" method="post". Login.php page had the php script that basically said if the form is not filled out, exit with an error.

I used the same script for the createaccount.php page and even with the form filled out, it is exiting with the error rather than continuing to the rest of the script?

I feel like this is something really obvious that I am missing but I have just spent too much time with this lol.

Form HTML

    <form class="form form_hidden" id="createAccount" action="createaccount.php" method="post">
      <h1 class="form_title">Create Account</h1>

      <div class="form_text error_message"></div>

      <input class="" type="text" placeholder="First Name" name="fname" id="fname" required />
        <div class="form_text input_error_message"></div>
      <input class="" type="text" placeholder="Last Name" name="lname" id="lname" required />
        <div class="form_text input_error_message"></div>
      <input class="" type="email" placeholder="Email Address" name="email" id="email" required />
        <div class="form_text input_error_message"></div>
      <input class="" type="tel" placeholder="(123) 456-7890" name="mobile_number" id="mobile_number" required />
        <div class="form_text input_error_message"></div>
      <input class="" type="password" placeholder="Password" name="password" id="password" required />
        <div class="form_text input_error_message"></div>
      <input class="" type="password" placeholder="Confirm Password" name="confirm_password" id="confirm_password" required />
        <div class="form_text input_error_message"></div>

      <button type="submit">Continue</button>

      <p class="form_text">
        Already have an account? <a href="./" id="linkLogin">Sign in!</a>
      </p>
    </form> 

PHP Script

  session_start();

  require_once "config.php";

  // file needs to be accessed via form submission
  if (!isset($_POST['fname'], $_POST['lname'], $_POST['email'], $_POST['mobile_number'], $_POST['password'], $_POST['confirm_password'])) {
    exit ('Please fill in all fields.');
  }

  // sql statement
  if ($stmt = $db->prepare('INSERT INTO accounts (fname, lname, email, mobile_number, password) VALUES (?, ?, ?, ?, ?)')) {

    mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_password, $param_fname, $param_lname, $param_mobile_number);

    // set parameters
    $param_email = $email;
    $param_password = password_hash($password, PASSWORD_DEFAULT);
    $param_fname = $fname;
    $param_lname = $lname;
    $param_mobile_number = $mobile_number;

    if(mysqli_stmt_execute($stmt)) {
      // redirect to login page
      header ("Location: index.php");
    } else {
      echo "Oops! Something went wrong! Please try again later.";
    }
  }

  mysqli_stmt_close($stmt);
*/

?>

CodePudding user response:

try using empty() like this

if(!(!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['mobile_number']) && !empty($_POST['password']) && !empty($_POST['confirm_password']))){{
exit ('Please fill in all fields.');
}
  • Related