dimanche 29 novembre 2020

Not receiving my php form data in my database when I register

I try adding a user registration to my page, but I am having issues getting any response from my database when I submit.

all I want is a system that can receive user data when they register, and I am able to see the data entered when registering on my database. Below are my codes

what am I doing wrong as this is the first time I am trying this.

thanks.

<?php require_once 'controllers/authController.php'; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="generator" content="Jekyll v4.0.1">
        <meta http-equip="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
        <link href="style.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
        <title>Love Family Network</title>
    </head>

        <body>
            <div class="navbar">
        
              <img src="logo.jpg" alt="family network" class="hands" width="80" height="80">
              <a class="active" href="index.html"><i class="home"></i> Home</a> 
              <a href="about.html"><i class="ab"></i> About</a> 
              <a href="#"><i class="ca"></i> Contact</a> 
               <a href="#"><i class="Do"></i> Donate</a> 
               <a href="signup.php"><i class="rel"></i>Sign Up</a>
              <a href="login.php"><i class="rel"></i> Login</a>
            </div>
            

              <form method="post" action="signup.php">  
                  <h2 class="text">Sign Up</h2> 

                    <div class="input-group">
                    <label>Username</label>
                    <input type="text" name="username" value="<?php echo $username; ?>" class="form">
                </div>
                <div class="input-group">
                    <label>Email</label>
                    <input type="text" name="email" value="<?php echo $email; ?>">
                </div>
                <div class="input-group">
                    <label>password</label>
                    <input type="password" name="password_1">
                </div>
                <div class="input-group">
                    <label>Confirm password</label>
                    <input type="password" name="password_2">
                </div>
                <div class="input-group">
                    <button type="submit" name="signup" class="btn">Sign Up</button>
                </div>
                <p>
                    Already a member? <a href="login.php">Sign in</a>
                </p>
              </form>
            </body>
            </html>

connection

<?php

session_start();

require 'config/db.php';

$errors = array();
$username = "";
$email = "";


//if user clicks on the sign up button
if(isset($POST['signup'])) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password_1'];
    $passwordConf = $_POST['password_2'];

    //validation
    if(empty($username)) {
        $errors['username'] = "Username required";
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $errors['email'] = "Email address is invalid";
    }

    if(empty($email)) {
        $errors['email'] = "Email required";
    }

    if(empty($password)) {
        $errors['password'] = "Password required";
    }

    if ($password !== $passwordConf) {
        $errors['password'] = "The two password do not match";
    }

    $emailQuery = "SELECT * FROM users WHERE email=? LIMIT 1";
    $stmt = $conn->prepare($emailQuery);
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $result =$stmt->get_result();
    $userCount = $result->num_rows;
    $stmt->close();

    if ($userCount > 0) {
        $errors['email'] = "Email already exist";
    }

    if (count($errors) ===0) {
        $password = password_hash($password, PASSWORD_DEFAULT);
        $token = bin2hex(random_bytes(50));
        $verified = false;

        $sql = "INSERT INTO users (username, email, verified, token, password) VALUES (?, ?, ?, ?, ?)";
         $stmt = $conn->prepare($sql);
         $stmt->bind_param('ssbss', $username, $email, $verified, $token, $password);

         if ($stmt->execute()) {
          // login user
          $user_id = $conn->insert_id;
          $_SESSION['id'] = $user_id;
          $_SESSION['username'] = $username;
          $_SESSION['email'] = $email;
          $_SESSION['verified'] = $verified;
          // set flash message
          $_SESSION['message'] = "You are now logged in!";
          $_SESSION['alert'] = "alert-success";
          header('location: homep.php');
          exit();
         } else {
            $errors['db_error'] = "Database error: failed to register";
         }
    }
}



Aucun commentaire:

Enregistrer un commentaire