Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a predicament that I'd love your help with if you have any insight. In Short, I'm currently doing a study portal type project and I'm looking to have it fully database driven so that when I register for a module, that particular module appears on the user's profile because as of right now, it doesn't matter if tom, dick or harry logs in, they all see the same three modules for a particular course.

So upon registering, a user would choose a course, a list of relevant modules would appear and then a user would choose their modules. From that, their homepage would be populated by the html & CSS code attached to that module.

There's 3 tables that interact with this section. UserDemo, course and module.

Here is the code I have to do with registering as a user currently (the course & modules must be added)

accounts\signup.php

<?php 
    include '../inc/db.php';
    include '../inc/functions.php';


    if (isset($_POST['signup'])) {
        $fName = p_s($_POST['fName']);
        $lName = p_s($_POST['lName']);
        $email = p_s($_POST['email']);
        $password = p_s($_POST['password']);
        $rpassword = p_s($_POST['rpassword']);
        $contentID = p_s($_POST['contentID']);
        if (!empty($fName) && !empty($lName) && !empty($email) && !empty($password) && !empty($contentID)) {
            if (strlen($password) === strlen($rpassword)) {

          $options = [
              'cost' => 12,
          ];
          $password = password_hash($password, PASSWORD_BCRYPT, $options);
          $created_at = date('Y-m-d G:i:s');

                $sql = "INSERT INTO usersDemo (fName, lName, email, password, contentID, status, created_at) VALUES ('$fName','$lName', '$email', '$password', '$contentID', 'approved', '$created_at')";
                if (mysqli_query($conn, $sql)) {
                    header('Location: ../signup.php?suc');exit();
                }
            }else{
                header('Location: ../signup.php?fidpass');exit();
            }
        }else{
            header('Location: ../signup.php?fempt');exit();
        }


    }


inc\signup.php


<form action="accounts/signup.php" method="POST">

      <div id="fade-box">
        <h2>Register</h2>
     <div class="form-group">
       <input name="fName" type="text" class="form-control" id="fName" placeholder="Enter your first name" required>
     </div>
     <div class="form-group">
       <input name="lName" type="text" class="form-control" id="lName" placeholder="Enter your last name" required>
     </div>
     <div class="form-group">
       <input name="email" type="email" class="form-control" id="email" placeholder="Email" required>
     </div>
     <div class="form-group">
       <input name="password" type="password" class="form-control" id="password" placeholder="Password" required>
     </div>
     <div class="form-group">
       <input name="rpassword" type="password" class="form-control" id="rpassword" placeholder="Repeat Password" required>
     </div>
     <div class="form-group">
       <input name="contentID" type="text" class="form-control" id="contentID" placeholder="Enter your contentID" required>
     </div>
     <button name="signup" type="submit" class="btn btn-success">Register</button>
     </div>
   </form>



Global Functions for login validation

function checkIfEmailExist($email){
  global $conn;
  $data[]=array();
  $sql = "SELECT userID FROM usersDemo WHERE email ='$email'";
  $run = mysqli_query($conn, $sql);
  $rows =mysqli_num_rows($run);
  if ($rows == 0) {
    return false;
  }else{
    return true;
  }
}


function checkPassword($email, $password){
  global $conn;
  $sql = "SELECT password FROM usersDemo WHERE email ='$email'";
  $run = mysqli_query($conn, $sql);
  while ($rows = mysqli_fetch_assoc($run)) {
    $hashedDBPass = $rows['password'];
  }
  if (password_verify($password, $hashedDBPass)) {
    return true;
  }else{
    return false;
  }
}


What I have tried:

This is my current plan of attack:


Select module name, module description from db

Display on screen

Iterate through values and display

Copy code for UI into for loop

What would your opinions be on this plan of attack or does anyone know of an alternative (and maybe easier) way of accomplishing this.

Thanks in advance for your time and help :)
Posted
Updated 6-Mar-18 11:05am

1 solution

PHP
$sql = "INSERT INTO usersDemo (fName, lName, email, password, contentID, status, created_at) VALUES ('$fName','$lName', '$email', '$password', '$contentID', 'approved', '$created_at')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900