Learn PHP MySQL in 2 Days : Day 2 of 2

      1 Comment on Learn PHP MySQL in 2 Days : Day 2 of 2

Learn PHP MySQL in 2 Days

3

I will explain with code in Day 2. Here I have created one application for all.  My First PHP Sample Application is contains following pages:

If you missed,  PHP Programming Fundamental.

index.php : This Page is home page to login user with available username and password.

page2.php : This page will verify  username and password which is passed from the index.php page and will verify with database and if it is correct you need to redirect user to some page and if it is wrong you need to redirect user back to index.php page for asking userid and password again.

home.php : This page is home page for the logged in users. If user passes valid userid and password then he/she will be redirected to home.php page. This page is also having some menu for user operations.

changepass.php : This page is added so that use can change their password.

changepass2.php : This page will receive password from the changepass.php and verify against database with existing one if current password is Same it will change password with new one,  and will redirect again back to changepass.php page.

profile.php : This page is to display profile details to user and user can also change their details using the same page.  Here another feature is important that it also fetches current information from the database as per the active session.

profile2.php : This page is act same as changepass2.php but here it will change profile details of users.

logout.php : This page is used to destroy session and redirect user back to index.php page.

Note: This code is only for beginners to learn PHP. There are new version of code available in PHP like object based that is suggested for Live Websites.

 

 Now Let Start with Actual Coding with PHP

Here I have to take few assumptions:

  • Database : MySQL
  • MySQL Database Name: fdp
  • MySQL Database Username : root
  • MySQL Database Password : root

Step 1 : Create Database using phpmyadmin or from command prompt

(DatabaseName : fdp)

MySQL Database Query :

CREATE TABLE IF NOT EXISTS `user_master` (
`uid` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Email` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

ALTER TABLE `user_master`
 ADD PRIMARY KEY (`uid`);

ALTER TABLE `user_master`
MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1;

Step 2 : Insert few records or execute following query.

 Sample Insert Query :

INSERT INTO `user_master` (`uid`, `username`, `password`, `Name`, `Email`) VALUES
(1, 'admin', 'admin', 'Adarsh Patel', 'noreply@adarshspatel.in'),
(2, 'user', 'user', 'Patel Adarsh', 'adarsh@google.com');

Step 3 : Create all following files and put it on Server and after execute it.

(Hint for WAMP Users : Save to www\fdp folder : url to run: http://localhost/fdp)

(Hint for XAMPP Users : Save to htdocs\fdp folder : url to run: http://localhost/fdp)

 index.php File Code

<?php session_start(); ?>

<html>
	<head>
		<title>	Login</title>
	</head>
	<body>
	<center>
		<form method="post" action="page2.php">
		<table>
			<tr>
				<td>	Username : </td>
				<td>	<input type="text" name="username"> </td>
			</tr>
			
			<tr>
				<td>	Password : </td>
				<td>	<input type="password" name="password"> </td>
			</tr>
			
			<tr>
				<td colspan="2"><center><input type="submit" value="Login"></center> </td>
			</tr>
			
			<tr>
			
				<td colspan="2"><center>	
					<?php
						if(isset($_SESSION['error']))
						{
							echo $_SESSION['error'];
							unset($_SESSION['error']);
						}
						
						if(isset($_GET['logout']))
						{
							echo "You have Succesfully Logged Out.";
							unset($_GET['logout']);
						}
					?>
				</center> </td>
			</tr>
		</table>
		</form>
	</center>
		
	</body>
</html>

index.php Screenshots

Index Page

page2.php File Code

<?php session_start();

//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "fdp";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Read Form Data from Page1
$u = $_POST['username'];
$p = $_POST['password'];


$query = "select * from user_master where username='$u' and password='$p'";
$result = mysql_query($query);

$affected_rows = mysql_affected_rows();

if($affected_rows==1)	//Login Successful
{
	$_SESSION['user']=$u;
	header('Location: home.php');
}
else	//Invalid Userid or Password
{
	$_SESSION['error']="Invalid Userid/Password";
	header('Location: index.php');
}
?>

 home.php File Code

<?php session_start(); ?>

<html>
	<head>
		<title>	Home</title>
	</head>
	<body bgcolor="#E0E0E0">

	Welcome <?php echo $_SESSION['user']; ?>,
	<hr size="2">
	<p align="right"> <a href="logout.php"> Logout </a> </p> 
	<hr size="2">
		<a href="home.php"> Home </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="profile.php"> Profile </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="changepass.php"> Change Password </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<hr size="2">
	<center>
	<h2>
	Welcome to FDP on PHP / MySQL
	</h2>
	</center>
		
	</body>
</html>

Home Page Screenshots

Home Page

 

 profile.php File Code

<?php session_start();


//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "fdp";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Check Page1 username and password with our user_master table.
$query = "select * from user_master where username='".$_SESSION['user']."'";
$result = mysql_query($query);

//Find number of Rows Return by Above Query.
$affected_rows = mysql_affected_rows();

if($affected_rows==1)		
{
	$row = mysql_fetch_array($result);
	
	$id=$row['uid'];
	$name=$row['Name'];
	$email=$row['Email'];
}
?>

<html>
	<head>
		<title>	Profile</title>
	</head>
	<body bgcolor="#E0E0E0">
	
	
	Welcome <?php echo $_SESSION['user']; ?>,
	<hr size="2">
	<p align="right"> <a href="logout.php"> Logout </a> </p> 
	<hr size="2">
		<a href="home.php"> Home </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="profile.php"> Profile </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="changepass.php"> Change Password </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<hr size="2">
<center>
		<p>Change Your Profile</p>

		<form method="post" action="profile2.php">
		<table>
			<tr>
				<td>	Your ID : </td>
				<td>	<input type="text" readonly="true" name="id" value="<?php echo $id; ?>"> </td>
			</tr>
			
			<tr>
				<td>	Name : </td>
				<td>	<input type="text" name="nm" value="<?php echo $name; ?>"> </td>
			</tr>
			
			<tr>
				<td>	Email Address : </td>
				<td>	<input type="text" name="email" value="<?php echo $email; ?>"> </td>
			</tr>
			
			<tr>
				<td colspan="2"><center>	<input type="submit" value="Update"></center> </td>
			</tr>
			
			<tr>
				<td colspan="2"><center>	
					<?php
						if(isset($_SESSION['profilemsg']))
						{
							echo $_SESSION['profilemsg'];
							unset($_SESSION['profilemsg']);
						}
					?>
				</center> </td>
			</tr>
		</table>
		</form>
	</center>		
	</body>
</html>

Profile Screenshots

Profile

profile2.php File Code

<?php session_start();

//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "fdp";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Read Form Data from Page1
$name = $_POST['nm'];
$email = $_POST['email'];

$query = "update user_master set name='$name',email='$email' where username='".$_SESSION['user']."'";
$result = mysql_query($query);

//Find number of Rows Return by Above Query.
$affected_rows = mysql_affected_rows();

if($affected_rows==1)		
{
	$_SESSION['profilemsg']="Profile Updated!";
	header('Location: profile.php');
}
else					
{
	$_SESSION['profilemsg']="Try Again";
	header('Location: profile.php');
}
?>

 

changepass.php File Code

<?php session_start(); ?>

<html>
	<head>
		<title>	Change Password</title>
	</head>
	<body bgcolor="#E0E0E0">
	
	Welcome <?php echo $_SESSION['user']; ?>,
	<hr size="2">
	<p align="right"> <a href="logout.php"> Logout </a> </p> 
	<hr size="2">
		<a href="home.php"> Home </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="profile.php"> Profile </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="changepass.php"> Change Password </a>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<hr size="2">
	
	<center>
		<p>Change Your Password</p>
	
		<form method="post" action="changepass2.php">
		<table>
			<tr>
				<td>	Current Password : </td>
				<td>	<input type="password" name="p1"> </td>
			</tr>
			
			<tr>
				<td>	New Password : </td>
				<td>	<input type="password" name="p2"> </td>
			</tr>
			
			<tr>
				<td>	Retype Password : </td>
				<td>	<input type="password" name="p3"> </td>
			</tr>
			
			
			<tr>
				<td colspan="2"><center>	<input type="submit" value="Change Password"></center> </td>
			</tr>
			
			<tr>
				<td colspan="2"><center>	
					<?php
						if(isset($_SESSION['passwordmsg']))
						{
							echo $_SESSION['passwordmsg'];
							unset($_SESSION['passwordmsg']);
						}
					?>
				
				</center> </td>
			</tr>
		</table>
		</form>
	</center>
		
	</body>
</html>

 changepass Screenshots

Change Password

 changepass2.php File Code

<?php session_start();


//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "fdp";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Read Form Data from Page1
$p1 = $_POST['p1'];
$p2 = $_POST['p2'];
$p3 = $_POST['p3'];

if($p2!=$p3)
{
	$_SESSION['passwordmsg']="New and Retype Password don't Match";
	header('Location: changepass.php');
}
//Check Page1 username and password with our user_master table.
$query = "update user_master set password='p3' where username='".$_SESSION['user']."' and password='$p1'";
$result = mysql_query($query);

//Find number of Rows Return by Above Query.
$affected_rows = mysql_affected_rows();

if($affected_rows==1)		
{
	$_SESSION['passwordmsg']="Password Changed !";
	header('Location: changepass.php');
}
else					
{
	$_SESSION['passwordmsg']="Invalid Current Password";
	header('Location: changepass.php');
}
?>

 

logout.php File Code

<?php session_start();
	session_destroy();
	header('Location: index.php?logout=true');
?>

 Logout Screen Shots

Logout

Hope this code will help you.  for feedback / suggestions / error kindly send email via contact us page.

1 thought on “Learn PHP MySQL in 2 Days : Day 2 of 2

  1. Pingback: php file upload

Leave a Reply