Tuesday, March 6, 2018

Login With Name and Pictures in PHP and MySQLi

An application that I improve from my previous work that I added display of user name with pictures that I wrote in PHP and MySQLi. This is a simple login system that is database. I hope you find my work useful. 

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.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.


















Sample Program Output


Program Listing

connect_to_database.php

<?php  
$localhost = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "login"; 

// create connection 
$connect = new mysqli($localhost, $username, $password, $dbname); 

// check connection 
if($connect->connect_error) {
    die("connection failed : " . $connect->connect_error);
} else {
    // echo "Successfully Connected";
}
  
?>

home.php

<?php
include 'connect_to_database.php'; //connect the connection page

if(empty($_SESSION)) // if the session not yet started 
   session_start();

if(!isset($_SESSION['username'])) { //if not yet logged in
   header("Location: login.php");// send to login page
   exit;
?>
<html>
<body>
<style>

.avatar {
    vertical-align: middle;
    width: 252px;
    height: 220px;
    border-radius: 150%;
border:5px solid white;
    border-radius: 600px;
    -webkit-border-radius: 600px;
    -moz-border-radius: 600px;
}

body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
    }
input, button, select, option, textarea {
    font-size: 100%;
}
</style>
<br>
<H2> Main Page </H2>
 <img src="<?php echo $_SESSION['pix']; ?>" alt="It's my picture"  class="avatar"/>
<br><br>
Welcome  <b> <?php echo $_SESSION['firstname']. " ".$_SESSION['lastname']."."; ?>  </b>

<br><br><br><br>
 <a href="logout.php">Logout</a> 
</body>
</html> 

login.php

<?php
include 'connect_to_database.php'; //connect the connection page
if(empty($_SESSION)) // if the session not yet started 
   session_start();


if(isset($_SESSION['username'])) { // if already login
   header("location: home.php"); // send to home page
   exit; 
}

?>
<html>
<head></head>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
   input, button, select, option, textarea {
    font-size: 100%;
}

</style>
<br>
<h2> Login System with Pictures in PHP and MySQLi</h2>
<form action = 'login_process.php' method='POST'>
  Enter   Username:   &nbsp;
 <input type="text" name="username" />  <br><br>
    Enter Password: &nbsp;
 <input type="password" name="password" />
<br> <br>
<input type = "submit" name="submit" value="Ok" />  
</form>
</body>
</html>


login_process.php

<html>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
</style>
<?php
error_reporting(0);
include 'connect_to_database.php'; //connect the connection page
  
  $username = $_POST['username'];
  $password = $_POST['password'];
if(empty($_SESSION)) // if the session not yet started 
   session_start();
if(!isset($_POST['submit'])) { // if the form not yet submitted
   header("Location: login.php");
   exit; 
}

  $query = "SELECT * FROM `users` WHERE username='$username'
and password='$password'";
$result = mysqli_query($connect,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
        if($rows==1){
              
    $row2  = mysqli_fetch_array($result);
               
        $_SESSION['username'] = $_POST['username'];
      
                 $_SESSION['lastname'] = $row2['lastname'];
         $_SESSION['firstname']= $row2['firstname'];
$_SESSION['pix'] =$row2['pix'];
                 header("Location: home.php");
             
        } else{ // if not a valid user  
            echo "<br>";
            echo "<h2> Invalid Password !!! Try Again </h2>"; 
            echo "<br>";
            echo "<font name='arial'> <font size='5'>";
            echo "<a href='logout.php'>Return To Login Page</a> " ;
            echo "</font></font>";
        }
   

?>

</body>
</html>


logout.php

<?php
session_start();
unset($_SESSION['username']);
session_destroy();

header("Location: login.php");
exit;
?>


users.sql

-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 06, 2018 at 09:39 AM
-- Server version: 10.1.30-MariaDB
-- PHP Version: 7.2.2

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: `login`
--

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

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

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

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

INSERT INTO `users` (`id`, `username`, `password`, `lastname`, `firstname`, `pix`) VALUES
(1, 'jake', 'jake', 'POMPERADA', 'JAKE', '/login/images/jake.jpg'),
(2, '123', '123', 'POMPERADA', 'JACOB SAMUEL', '/login/images/jacob.jpg'),
(3, 'iya', 'iya', 'POMPERADA', 'JULIANNA RAE', '/login/images/iya.jpg'),
(4, 'allie', 'allie', 'POMPERADA', 'MA. JUNALLIE', '/login/images/allie.jpg'),
(5, 'bill', 'bill', 'GATES', 'WILLIAM', '/login/images/gates.jpg'),
(6, 'peter', 'peter', 'NORTON', 'PETER', '/login/images/norton.jpg');

--
-- Indexes for dumped tables
--

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

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
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 */;





Monday, March 5, 2018

User Record System in PHP and MySQLI

A simple crud application that I wrote using PHP and MySQLI which is already compliant with the new version of PHP.  

My email address are the following jakerpomperada@gmail.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.
 







Sample Program Output


Program Listing

add.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
    <title>User Record System Using PHP and MySQLI</title>


</head>
<body>

<?php
 require_once 'connections.php';

if($_POST) {
    $name = $_POST['name'];
    $age = $_POST['age'];

    $sql = "INSERT INTO person (name,age) VALUES ('$name', '$age')";
    if($connect->query($sql) === TRUE) {
echo "<p>New Record Successfully Created</p>";
        echo "<a href='create.php'><button type='button'>Back</button></a>";
        echo "<a href='index.php'><button type='button'>Home</button></a>";
    } else {
        echo "Error " . $sql . ' ' . $connect->connect_error;
    }

    $connect->close();
}

  
?>
</body>
</html>


delete.php

<?php 

require_once 'connections.php';

if($_GET['id']) {
$id = $_GET['id'];

$sql = "SELECT * FROM person WHERE id = {$id}";
$result = $connect->query($sql);
$data = $result->fetch_assoc();

$connect->close();
?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>Delete User Record</title>
</head>
<body>

<h3>Do you really want to delete the record of
<?php echo strtoupper($data['name'].'?');  ?>
</h3>
<form action="remove.php" method="post">

<input type="hidden" name="id" value="<?php echo $data['id'] ?>" />
<button type="submit">Ok</button>
<a href="index.php"><button type="button">Back</button></a>
</form>

</body>
</html>

<?php
}
?>

edit.php

<?php 

require_once 'connections.php';

if($_GET['id']) {
$id = $_GET['id'];

$sql = "SELECT * FROM person WHERE id = {$id}";
$result = $connect->query($sql);

$data = $result->fetch_assoc();

$connect->close();

?>

<!DOCTYPE html>
<html>
<head>
<title>Edit Users</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>

<fieldset>
<legend>Edit Users</legend>

<form action="update.php" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<th>Name</th>
<td><input type="text" name="name" placeholder="Name" value="<?php echo $data['name'] ?>" /></td>
</tr>
<tr>
<th>Age</th>
<td><input type="text" name="age" placeholder="Age" value="<?php echo $data['age'] ?>" /></td>
</tr>
<tr>
<input type="hidden" name="id" value="<?php echo $data['id']?>" />
<td ><button id="someTable" type="submit">Save Changes</button>&nbsp;&nbsp;
<a href="index.php"><button type="button">Back</button></a></td>
</tr>
</table>
</form>

</fieldset>

</body>
</html>

<?php
}
?>


index.php

<html>
<?php require_once 'connections.php'; ?>
<!DOCTYPE html>
<<br>
 <h3 align="center"> User Record System Using PHP and MySQLI </h3>html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
    <title>User Record System Using PHP and MySQLI</title>


</head>
<body>
 <br>
<div class="manageUser">
    <a href="create.php"><button type="button">Add User</button></a>
    <table border="1" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
<th>Option</th>
                
            </tr>
        </thead>
        <tbody>
             <?php
            $sql = "SELECT * FROM person";
            $result = $connect->query($sql);

            if($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "<tr>
                        <td>".$row['name']."</td>
                        <td>".$row['age']."</td>
                         <td>
                            <a href='edit.php?id=".$row['id']."'><button type='button'>Edit</button></a>
                            <a href='delete.php?id=".$row['id']."'><button type='button'>Remove</button></a>
                        </td>
                    </tr>";
                }
            } else {
                echo "<tr><td colspan='5'><center>No Data Avaliable</center></td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>

</body>
</html>

update.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
    <title>User Record System Using PHP and MySQLI</title>


</head>
<body>

<?php 

require_once 'connections.php';

if($_POST) {
$name = $_POST['name'];
$age = $_POST['age'];
$id = $_POST['id'];

$sql  = "UPDATE person SET name = '$name',age = '$age' WHERE id = {$id}";
if($connect->query($sql) === TRUE) {
echo "<p>Succcessfully Updated</p>";
echo "<a href='edit.php?id=".$id."'><button type='button'>Back</button></a>";
echo "<a href='index.php'><button type='button'>Home</button></a>";
} else {
echo "Erorr while updating record : ". $connect->error;
}

$connect->close();

}

?>

</body>
</html>


connections.php

<?php  
$localhost = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "user"; 

// create connection 
$connect = new mysqli($localhost, $username, $password, $dbname); 

// check connection 
if($connect->connect_error) {
    die("connection failed : " . $connect->connect_error);
} else {
    // echo "Successfully Connected";
}
  
?>


create.php

<!DOCTYPE html>
<html>
<head>
    <title>Add Member</title>

    <link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>

<fieldset>
    <legend>Add User</legend>

    <form action="add.php" method="post">
        <table cellspacing="0" cellpadding="0">
            <tr>
                <th>Name</th>
                <td><input type="text" name="name" placeholder="Name" /></td>
            </tr>     
            <tr>
                <th>Age</th>
                <td><input type="text" name="age" placeholder="Age" /></td>
            </tr>
            
            <tr>
                <td><button type="submit">Save Changes</button></td>
                <td><a href="index.php"><button type="button">Back</button></a></td>
            </tr>
        </table>
    </form>

</fieldset>

</body>
</html>

remove.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
    <title>User Record System Using PHP and MySQLI</title>


</head>
<body>
<?php 

require_once 'connections.php';

if($_POST) {
$id = $_POST['id'];

$sql = "DELETE FROM person WHERE  id = {$id}";
if($connect->query($sql) === TRUE) {
echo "<link rel='stylesheet' type='text/css href='mystyle.css'>";
echo "<p>Record Sucessfully Remove from the Database !!!</p>";
echo "<a href='index.php'><button type='button'>Back</button></a>";
} else {
echo "Error removing record : " . $connect->error;
}

$connect->close();
}

?>

</body>
<html>

mystyle.css


            
        .manageUser {
            width: 50%;
            margin: auto;
        }

        table {
font-family:arial;
font-weight:bold;
size:18px;
            width: 100%;
            margin-top: 20px;
        }

        body {
background-color:lightgreen;
font-family:arial;
font-weight:bold;
size:18px;
}
fieldset {
margin: auto;
margin-top: 100px;
width: 50%;
}

table tr th {
padding-top: 10px;
}
user.sql

-- phpMyAdmin SQL Dump -- version 3.1.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Jan 04, 2014 at 06:55 AM -- Server version: 5.1.30 -- PHP Version: 5.2.8 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!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 utf8 */; -- -- Database: `user` -- -- -------------------------------------------------------- -- -- Table structure for table `person` -- CREATE TABLE IF NOT EXISTS `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `age` int(3) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Dumping data for table `person` -- INSERT INTO `person` (`id`, `name`, `age`) VALUES (1, 'Manuel Uy', 36), (2, 'Jorge Vargas', 12), (3, 'Maria Buenavista', 22);