0

I am working on a school portal project and want to connect my signup form to MySQL database using PHP but its not storing data while submitting it. Constantly getting Undefined index error.

Following is my HTML form code:

 <form action="signup.php" method="POST">
                      <div class="form-row">
                          <div class="col-lg-7">
                              <input type="email" class="form-control my-3 p-4" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" placeholder="Email-Address" style="border: 1px solid;" name="email"/>
                          </div>
                      </div>
                      <div class="form-row">
                        <div class="col-lg-7">
                            <input type="password" class="form-control my-3 p-4" style="border: 1px solid;" required  placeholder="Password" pattern="{8,}" name="password"/>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-lg-7">
                            <input type="text" class="form-control my-3 p-4" style="border: 1px solid;" required  placeholder="Username" name="username"/>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-lg-7">
                            <button type="button" class="btn1 mt-3 mb-3">Register</button>
                        </div>
                    </div>
                    <p>Already have an account? <a href="login.html"> Login</a></p>
                  </form>
              </div>

Following is my PHP code:

    <?php 
$email_addr=$_POST ['email']; //error for this line 
$password=$_POST['password']; //error for this line 
$username=$_POST ['username']; //error for this line 

$connection= new mysqli('localhost','root','','BHS');
if ($connection->connect_error){
    die('COnnection Failed: '.$connection->connect_error);

}else{
    $stmt= $connection->prepare("Insert into signup (no,email,password,username) values (?,?,?,?)");
    $stmt->bind_param("isss",$no,$email,$password,$username);
    $stmt->execute;
    echo "Registered successfully";
    $stmt->close();
    $connection->close();
}

?>
Waleed
  • 3
  • 2
  • You need an `input` of type `submit` to submit the form. You should also give it a `name` and check for its presence in the posted data: `if (isset($_POST['your_button_name_here']))` before you perform any other actions. – El_Vanja Dec 10 '20 at 17:41
  • 1
    And be it a school project or not, you should never store passwords as plain text. Use PHP's built-in [`password_hash`](https://www.php.net/manual/en/function.password-hash) and [`password_verify`](https://www.php.net/manual/en/function.password-verify) to safely store and check passwords. Good habits are made early. – El_Vanja Dec 10 '20 at 17:42

1 Answers1

0

If you have both the PHP and HTML on the same page like that, that means the PHP is running no matter if submit the form or not. Also since your Register button is of type button unless you have a javascript event handler, that form isn't getting submitted by clicking register.

Try wrapping your php in

if(sizeof($_POST) > 0){
   //your php
}
imvain2
  • 15,480
  • 1
  • 16
  • 21