Programming Tutorial #4: Manipulating DB (Signup / Login in PHP)

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn how to properly query a database
  • How to create a basic login system
  • How to use PHP

Requirements

  • WAMP / XAMPP / LAMP
  • Notepad++ or PHPStorm
  • Previous Tutorial Code

Difficulty

  • Basic

Tutorial Contents

So, today we will make the basis of the interaction of the database

Firstly, create a file called DatabaseConnect.php in the scripts folder and start the file and import the previously created configuration:

<?php
$config = include 'config.php';

Next, we will create the function to create a new user:

function newRecord($username,$password,$email,$displayName) {
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("INSERT INTO users (username, password, email, display) VALUES (:username, :password, :email, :display)");
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':display', $displayName);
    $stmt->execute();
}

This code creates a premade query, assigns values to it, and then sends it to the database

function updatepass($user,$pass) {
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("UPDATE `users` SET `password`= :password WHERE `username` = :username");
    $stmt->bindValue(':username',$user);
    $stmt->bindValue(':password',$pass);
    $stmt->execute();
}

This allows a user to change their password, please note, these have no validation, we will add that later.

function usernameExists($username) {
    //Checks Whether A Username Exists (Returns 0 or 1 / true or false)
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $stmt = $conn->prepare('select * from users where `username` = :userx');
    $stmt->bindValue(':userx',$username);
    $stmt->execute();
    $isvalidtest = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if (sizeof($isvalidtest)>0) {
        return true;
    } else {
        return false;
    }
}

Checks if a username already exists, so that users can't take an existing account.

function emailExists($email) {
    //Checks Whether An Email Exists (Returns 0 or 1 / true or false)
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $stmt = $conn->prepare('select * from users where `email` = :emailx');
    $stmt->bindValue(':emailx',$email);
    $stmt->execute();
    $isvalidtest = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if (sizeof($isvalidtest)>0) {
        return true;
    } else {
        return false;
    }
}

Same as above, but for emails.

function getrelatedinfo($username) {
    //Please  use isusernameinexistance first, then use this.
    //Returns Array With Details
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $stmt = $conn->prepare('select * from users where `username` = :userx');
    $stmt->bindValue(':userx',$username);
    $stmt->execute();
    return ($stmt->fetchAll(PDO::FETCH_ASSOC));
};

Gets all info related to a certain username

function getrelatedinfoe($email) {
    //Please  use isusernameinexistance first, then use this.
    //Returns Array With Details
    $config = include 'config.php';
    $conn = new PDO("mysql:host=$config->host;dbname=$config->database", $config->username,$config->pass);
    $stmt = $conn->prepare('select * from users where `email` = :emailx');
    $stmt->bindValue(':emailx',$email);
    $stmt->execute();
    return ($stmt->fetchAll(PDO::FETCH_ASSOC));
}

Gets related details to a certain email address.

function splitVar($atestx) {
foreach ($atestx as $row => $g) {
            $pass=$g['password'];
            $ema=$g['email'];
            $usr=$g['username'];
}
return array($pass,$ema,$usr);};

Splits info from getrelatedinfo & getrelatedinfoe

Thanks for reading, next week we will create filters for the users and passwords, and the actual pages.

This lesson's code is on github

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Contributors should not ask for Steem/Steemit related activities in their posts, such as upvotes, resteems and follows.

If you remove this (everything below the curriculum) your contribution may still be accepted.

You can contact us on Discord.
[utopian-moderator]

Hey @amosbastian, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

I think I have fixed it @amosbastian

Good job, I will get my supervisor to approve the post!

Thanks & Have a nice day!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @cadawg I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.33
TRX 0.11
JST 0.034
BTC 66407.27
ETH 3219.07
USDT 1.00
SBD 4.34