Search code examples
phpmysqldatabaseverification

Storing data entered when signing up


I need to create a sign up page that will store user name email passwords and put them in a database so that the user can then login and access a profile etc.

I have made a database database however nothing will go into it. I input one manually but anything I try to do from the webpage won't go to the database.

Code for the webpage: Signup is the page I want displayed and adduser is the code for adding the data to the database.

Signup:

    <?php include '../view/header.php';
 ?>
<br>
<br>
<h1 class="light white-text text-lighten-3">Sign up!</h1>
<br>
<br>
<form class="form" id="signup" action="addUser.php" method="post">  


     <div class="form-group ">
        <label for="email">Email</label>
        <input type="email" class="form-control" id="email" name="email" placeholder="Enter Your Email">
    </div>
    <br>
      <div class="form-group ">
            <input id="user_name" type="text" class="validate" name="user_name"required="required">
          <label for="user_name">User Name</label>
        </div>
    <br>
      <div class="form-group col s6">
        <label for="password">Password</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="Enter a Password">
    </div>
    <br>

    <br>
     <button type="submit" class="orange btn btn-primary">Submit</button>  
</form>  


<?php

include '../view/footer.php';

AddUser:

<script src="../js/materialize.js" type="text/javascript"></script>
<script src="../js/materialize.min.js" type="text/javascript"></script>
<script src="../js/init.js" type="text/javascript"></script>
<?php

$server = "localhost";
$username = 'root';
$Password ="";
$database = 'commish';

$con = mysqli_connect($server, $username, $Password, $database);


$email    = filter_input(INPUT_POST, 'email');

$user_name    = filter_input(INPUT_POST, 'user_name');

$password    = filter_input(INPUT_POST, 'password'); 

new_user( $user_name, $password,$email, $con);
function new_user($user_name, $password, $email,$con) 
{
    global $con;
    $query = "INSERT into users (user_name, password, email) VALUES (:user_name, :password, :email)";
    $statement = $con->prepare($query);
    $statement->bindValue(":user_name", $user_name);
    $statement->bindValue(":password", $password);
    $statement->bindValue(":email", $email);
    $statement->execute();
    echo 'Successfully created new user';
}

Solution

  • There's no bindValue() method in mysqli, PDO has. So here are the two approaches to solve your problem:

    1)mysqli method:

    Use bind_param() method to bind variables to your prepared statement. So your new_user() function should be like this:

    function new_user($user_name, $password, $email,$con){
        $query = "INSERT into users (user_name, password, email) VALUES (?, ?, ?)";
        $statement = $con->prepare($query);
        $statement->bind_param("sss", $user_name, $password, $email);
        if($statement->execute()){
            echo 'Successfully created new user';
        }else{
            // query failed
        }
    }
    

    NOTE: Since you're passing the connection handler $con to this function, there's no need to use global $con;. Plus Globals are evil.


    2)PDO method:

    Keep your new_user() function as it is and change this line

    $con = mysqli_connect($server, $username, $Password, $database);
    

    to

    $con = new PDO("mysql:host=$server;dbname=$database",$username,$Password);
    

    Sidenote: Never store password as a plain readable text, always perform salted password hashing on raw password before inserting it into the table.