0

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";
         }
    }
}
David Abel
  • 41
  • 7
  • Do you get any errors? – El_Vanja Nov 29 '20 at 12:35
  • 1
    Also, `css` is irrelevant to this question. I'd suggest you edit the question to remove both the tag and the code. – El_Vanja Nov 29 '20 at 12:38
  • 1
    Your HTML doesn't correspond to your PHP code. Your form has `name="password_1"` and `name="password_2"`, but you use `$_POST['password']` and `$_POST['passwordConf']`. – El_Vanja Nov 29 '20 at 12:40
  • If you're not seeing "Undefined index" notices, check out [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Nov 29 '20 at 12:43
  • I have corrected it, but still getting the same issue. no data sent to my database – David Abel Nov 29 '20 at 15:14
  • Here's a thorough [guide](https://stackoverflow.com/a/22662582/4205384) on debugging db related problems. – El_Vanja Nov 29 '20 at 15:16

1 Answers1

0

In your signup.php file at the very start you have to change which POST parameters are being read for the password and passwordConf variables to match the name= parts on your form.

$_POST['password'] should be $_POST['password_1']

and

$_POST['passwordConf'] should be $_POST['password_2']

the resulting code should be:

<?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'];  // changed from 'password'
    $passwordConf = $_POST['password_2']; // changed from 'passwordConf'
mike510a
  • 1,936
  • 1
  • 9
  • 24