From fdad659fb4b988714318b3dedf0c529ecfbbfad4 Mon Sep 17 00:00:00 2001 From: Aamir Shahzad Date: Fri, 17 Nov 2023 09:24:40 +0500 Subject: [PATCH] Signup, login, logout and list record working. Started work on add record but not complete yet --- Dashboard.php | 9 ++ Example1/Add.php | 6 + Example1/Example1.php | 44 ++++++ Example1/index.php | 6 + Login.php | 11 ++ Logout.php | 9 ++ PRC.php | 352 ++++++++++++++++++++++++++++++++++++++++++ Signup.php | 11 ++ example.sql | 99 ++++++++++++ style.css | 4 + 10 files changed, 551 insertions(+) create mode 100644 Dashboard.php create mode 100644 Example1/Add.php create mode 100644 Example1/Example1.php create mode 100644 Example1/index.php create mode 100644 Login.php create mode 100644 Logout.php create mode 100644 PRC.php create mode 100644 Signup.php create mode 100644 example.sql create mode 100644 style.css diff --git a/Dashboard.php b/Dashboard.php new file mode 100644 index 0000000..53b1e7b --- /dev/null +++ b/Dashboard.php @@ -0,0 +1,9 @@ +dashboard(); \ No newline at end of file diff --git a/Example1/Add.php b/Example1/Add.php new file mode 100644 index 0000000..f88dd80 --- /dev/null +++ b/Example1/Add.php @@ -0,0 +1,6 @@ +add(); \ No newline at end of file diff --git a/Example1/Example1.php b/Example1/Example1.php new file mode 100644 index 0000000..2582e4b --- /dev/null +++ b/Example1/Example1.php @@ -0,0 +1,44 @@ +tblName = 'example1'; + $this->auth = "WHERE user_id = {$_SESSION['user_id']}"; + $this->tblCols = array( + 'id' => array( + 'display as' => 'ID', + 'is display' => array( + 'on listing' => true, + 'on add' => false, + 'on edit' => false, + ), + ), + 'name' => array( + 'display as' => 'Name', + 'is display' => array( + 'on listing' => true, + 'on add' => true, + 'on edit' => true, + ), + ), + 'date' => array( + 'display as' => 'Date', + 'is display' => array( + 'on listing' => true, + 'on add' => true, + 'on edit' => true, + ), + ), + ); + } // __construct() + + function __destruct() { + parent::__destruct(); + } // __destruct() +} // class \ No newline at end of file diff --git a/Example1/index.php b/Example1/index.php new file mode 100644 index 0000000..23e1d2c --- /dev/null +++ b/Example1/index.php @@ -0,0 +1,6 @@ +list(); \ No newline at end of file diff --git a/Login.php b/Login.php new file mode 100644 index 0000000..3610def --- /dev/null +++ b/Login.php @@ -0,0 +1,11 @@ + true, +)); +$prc->login(); \ No newline at end of file diff --git a/Logout.php b/Logout.php new file mode 100644 index 0000000..d20c745 --- /dev/null +++ b/Logout.php @@ -0,0 +1,9 @@ +logout(); \ No newline at end of file diff --git a/PRC.php b/PRC.php new file mode 100644 index 0000000..dd5611c --- /dev/null +++ b/PRC.php @@ -0,0 +1,352 @@ +appPathUrl}/Login.php?msg={$msg}&color={$color}"); + exit; + } + // ENDED - Redirect to login page if user is not logged-in + + $this->dbConn = mysqli_connect( + 'localhost', // servername/IP + 'root', // username + '', // password + 'example' // Database name, notice no "," + ); + + if ($this->showHTML) { + ?> + + + + + + + + + <?= $this->appNameShort.' - '.$this->appName ?> + + + +
+

appName ?>

+
+ + showHTML) + } // __construct() + + function list() + { + $headers = ''; + $fields = ''; + $displayColCount = 0; + + foreach ($this->tblCols as $key => $tblCol) { + if ($tblCol['is display']['on listing'] === false) { + continue; + } + + $headers .= "{$tblCol['display as']}"; + $fields .= "{$this->tblName}.{$key}, "; + $displayColCount++; + } // foreach (tblCols) + + $fields = rtrim($fields,', '); + + $sql = "SELECT $fields FROM {$this->tblName} {$this->auth};"; + // print_r($sql); + + $result = mysqli_query( + $this->dbConn, + $sql + ); + + if (mysqli_num_rows($result)) { + $row = mysqli_fetch_assoc($result); + $cols = array_keys($row); + // print_r($cols); + } // if ($result->num_rows) + + $tblNameUcF = ucfirst($this->tblName); + ?> +
+

+ (add new record)

+ + + + + Action'; + ?> + + + num_rows) + ?> + + + + tblCols as $key => $tblCol) { + if ($tblCol['is display']['on listing'] === false) { + continue; + } + + echo ""; + } // foreach (tblCols) + + echo ''; + ?> + + num_rows) + else { + ?> + + num_rows) + ?> + +
{$row[$key]}Edit | Delete
No record found
+
+

$_POST

'; + print_r($_POST); + echo ''; + ?> +
+

Add tblName,'s')) ?>

+
+ tblCols as $key => $tblCol) { + if ($tblCol['is display']['on add'] === false) { + continue; + } + ?> +
+ + +
+ +
+ +
+
+ dbConn, + "SELECT {$this->tblName}.$name FROM {$this->tblName} {$this->auth} AND {$this->tblName}.id = ? LIMIT 1;" + ); + + // print_r($result); + + mysqli_stmt_bind_param($stmt, "i", $id); + + if (mysqli_stmt_execute($stmt) === true) { + $result = mysqli_stmt_get_result($stmt); + $row = mysqli_fetch_array($result, MYSQLI_ASSOC); + $finfo = new finfo(FILEINFO_MIME_TYPE); + $mimeType = $finfo->buffer($row[$name]); + header("Content-type: $mimeType"); + echo $row[$name]; + } // if (mysqli_stmt_execute($stmt) === true) + } // blob() + + function signup() + { + // echo '

$_POST

'; + // print_r($_POST); + // echo '
'; + + if (!empty($_POST['email'])) { + $stmt = mysqli_prepare( + $this->dbConn, + 'INSERT INTO users VALUES (null, ?, ?, ?);' + ); + + mysqli_stmt_bind_param( + $stmt, + "sss", // i int, d float, s string + $_POST['name'], + $_POST['email'], + md5($_POST['password']), + ); + + mysqli_stmt_execute($stmt); + + $msg = 'Signup successfully, please login.'; + $color = 'green'; + header("Location: {$this->appPathUrl}/Login.php?msg={$msg}&color={$color}"); + exit; + } // if post + ?> +
+
+

Sign Up

+
+ + +
+
+ + +
+
+ + +
+ +
+
+ dbConn, + "SELECT id FROM users WHERE email=? AND password=? LIMIT 1" + ); + + $_POST['password'] = md5($_POST['password']); + + $stmt->bind_param( + "ss", + $_POST['email'], + $_POST['password'] + ); + + if ($stmt->execute() === true) { + $result = $stmt->get_result(); + $row = $result->fetch_assoc(); + + if (!empty($row)) { + $_SESSION['user_id'] = $row['id']; + $color = 'green'; + header("Location: {$this->appPathUrl}/Dashboard.php?msg={$msg}&color={$color}"); + exit(); + } + else { + $msg = 'Invalid email/password.'; + $color = 'red'; + header("Location: {$this->appPathUrl}/Login.php?msg={$msg}&color={$color}"); + exit(); + } + } // if ($stmt->execute() === true) + } // if post + ?> +
+
+

Login

+ {$_GET['msg']}

"; + $_GET['msg'] = ''; + $_GET['color'] = ''; + } // if GET msg + ?> +
+ + +
+
+ + +
+ + Signup +
+
+ +
+

Welcome to the dashboard.

+
+ appPathUrl}/Login.php?msg={$msg}&color={$color}"); + exit; + } // logout() + + function __destruct() { + if ($this->showHTML) { + ?> + + + + showHTML) + + mysqli_close($this->dbConn); // optional + } // __destruct() +} // class PRC \ No newline at end of file diff --git a/Signup.php b/Signup.php new file mode 100644 index 0000000..4ed6462 --- /dev/null +++ b/Signup.php @@ -0,0 +1,11 @@ + true, +)); +$prc->signup(); \ No newline at end of file diff --git a/example.sql b/example.sql new file mode 100644 index 0000000..3427fb6 --- /dev/null +++ b/example.sql @@ -0,0 +1,99 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.1 +-- https://www.phpmyadmin.net/ +-- +-- Host: localhost +-- Generation Time: Nov 17, 2023 at 05:16 AM +-- Server version: 10.4.28-MariaDB +-- PHP Version: 8.2.4 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +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: `example` +-- +CREATE DATABASE IF NOT EXISTS `example` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; +USE `example`; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `example1` +-- + +CREATE TABLE `example1` ( + `user_id` int(10) UNSIGNED NOT NULL, + `id` int(10) UNSIGNED NOT NULL, + `name` varchar(255) NOT NULL, + `date` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `users` +-- + +CREATE TABLE `users` ( + `id` int(10) UNSIGNED NOT NULL, + `name` varchar(255) DEFAULT NULL, + `email` varchar(255) NOT NULL, + `password` varchar(255) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `example1` +-- +ALTER TABLE `example1` + ADD PRIMARY KEY (`id`), + ADD KEY `user_id` (`user_id`); + +-- +-- Indexes for table `users` +-- +ALTER TABLE `users` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `email` (`email`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `example1` +-- +ALTER TABLE `example1` + MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT for table `users` +-- +ALTER TABLE `users` + MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT; + +-- +-- Constraints for dumped tables +-- + +-- +-- Constraints for table `example1` +-- +ALTER TABLE `example1` + ADD CONSTRAINT `example1_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; +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 */; diff --git a/style.css b/style.css new file mode 100644 index 0000000..8ba2cdd --- /dev/null +++ b/style.css @@ -0,0 +1,4 @@ +table, th, td { + border: 1px solid black; + border-collapse: collapse; +} \ No newline at end of file