Friday, March 8, 2019

Login and Registration in Python and MySQL

Here is a program that we wrote together with my best friend Rolly M. Moises a login and registration created using Python programming language and MySQL as our backend database. We wrote this code for our upcoming book on Python programming.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting.

work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com










Sample Program Output



Program Listing


# login_registration.py
# Rollyn M. Moises and Jake R. Pomperada
# March 8, 2019   Friday
# Bacolod City, Negros Occidental

import pymysql
import subprocess as sp
import getpass


class Login:

    def __init__(self):
        self.connection = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='',
            db='school_management',
        )

    def check_user(self, username, pwd):
        try:
            with self.connection.cursor() as cursor:
                sql = "SELECT * FROM users WHERE username = %s and password = %s"
                try:
                    rows = cursor.execute(sql, (username, pwd,))
                    if rows > 0:
                        record = cursor.fetchone()
                        print()
                        print("\tHello %s %s" % (record[3], record[4]),'.')
                        print()
                        print("\tWelcome to the System.")
                    else:
                        print("\tInvalid Username and/or Password. Try again.")
                except:
                    print("\tUnable to fetch records")

            self.connection.commit()
        except:
            self.connection.close()

    def login_screen(self):
        print()
        print("\tLOGIN SECURITY PAGE");
        print()
        user = input("\tEnter Username : ")
        password = getpass.getpass(prompt='\tEnter Password :')
        self.check_user(user, password)

    def logout(self):
        print("\n")
        print("\t\t\tTHANK YOU FOR USING THIS SOFTWARE.")
        print()
        print("\tCopyright 2019.  Product of Bacolod City, Negros Occidental Philippines.")
        print()
        self.connection.close()

    def register_user(self, username, password, firstname, lastname):
        try:
            with self.connection.cursor() as cursor:
                sql = "INSERT INTO users (`username`, `password`,`firstname`,`lastname`) VALUES (%s, %s,%s,%s)"
                try:
                    cursor.execute(sql, (username, password, firstname, lastname))
                    print()
                    print("\tRegistration Successfully Saved in the Database.")
                except:
                    print()
                    print("\tUnable to Register User.")
            self.connection.commit()
        except:
            self.connection.close()

    def register(self):
        print()
        print("\tLOGIN REGISTRATION PAGE");
        print()
        user = input("\tEnter Username : ")
        password = getpass.getpass(prompt='\tEnter Password : ')
        confirm_password = getpass.getpass(prompt='\tConfirm Password : ')
        print()
        first_name = input("\tEnter Firstname : ")
        last_name = input("\tEnter Lastname   : ")
        print()
        if password != confirm_password:
            print("\tPassword does not match. Try Again.")
        else:
            self.register_user(user, password, first_name.upper(),last_name.upper())


class Menu:

    def logout(self):
        self.login.logout()

    def selection(self):
        ch = "0"
        self.login = Login()
        while ch != "3":
            tmp = sp.call('cls', shell=True)
            print()
            print("\t\t    LOGIN AND REGISTRATION SYSTEM");
            print("\t\t\t     CREATED BY");
            print("\t\tROLLYN  M. MOISES AND JAKE R. POMPERADA")
            print()
            print("\t[1] Login Registered User")
            print("\t[2] Register New User")
            print("\t[3] Exit Program")
            print()
            ch = input("\tSelect option : ")
            menu_selection = {
                "1": self.login.login_screen,
                "2": self.login.register,
                "3": self.logout,
            }
            func = menu_selection.get(ch)
            if func is not None:
                func()
            else:
                print("\tInvalid option selected")
            print()
            input("\tPRESS ENTER KEY TO CONTINUE.")
menu = Menu()
menu.selection()


school_management.sql

-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 08, 2019 at 04:32 AM
-- Server version: 10.1.37-MariaDB
-- PHP Version: 7.3.0

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `school_management`
--

-- --------------------------------------------------------

--
-- Table structure for table `student`
--

CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `course` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`id`, `name`, `course`) VALUES
(8, 'Jake Rodriguez Pomperada', 'Bachelor of Science in Computer Science');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL,
  `firstname` varchar(100) DEFAULT NULL,
  `lastname` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `firstname`, `lastname`) VALUES
(4, 'admin', 'admin', 'JAKE', 'POMPERADA'),
(5, '123', '123', 'ROLLYN', 'MOISES');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `student`
--
ALTER TABLE `student`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `student`
--
ALTER TABLE `student`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


DOWNLOAD SOURCE CODE HERE


No comments:

Post a Comment