108PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

TYBCA-SEM-5-DIV-2 PHP Roll no.

-108
Create a website in PHP which have following pages.User Registration Page : (Apply proper validation)
Name: (*Only alphabet a(A) to z(Z) and space is allowed)
Password: (*Password must be between 5 to 10 characters)
Retype Password:
Mobile Number: (*Only 10 digit number is allowed) Hobbies: (Use checkbox)
Gender: (Use radio button)
City : (Use drop down select)
Home Address: (Use textarea)
Email Id : (*properemail address allowed)
Photo Upload : (*Must be less than 100kb)

User Login Page : (use session variable)


Email Id :
Password :

User Home Page :Display user data (registration data of user) with his/her photo who have just login.
Put link or button, click on which open edit page to edit data. (Email Id cannot change) èLogout link (destroysession
variable)

Admin Login Page : (use session variable)


User Name :
Password :

Admin Home Page :Display all users registration data (Whole table) with photo, edit and delete links with each record.
Also all column namemust have order link.Search data (Use Ajax)

REGISTRATION :
<html>
<body>
<?php

$nm=$p1=$p2=$mb=$gender=$hb1=$hb2=$hb3=$hb4=$hb=$ct=$ad=$email=$image="";
$nmErr=$p1Err=$p2Err=$mbErr=$genderErr=$hbErr=$ctErr=$adErr=$emailErr=$imageErr="";

if(isset($_POST['btnsub']))
{
$nm = $_POST['nam'];
if(empty($nm))
{
$nmErr = "* Name is required";
}
else
{
if (!preg_match ("/^[a-zA-Z\s]*$/", $nm) )
{
$nmErr = "Only alphabat charaters is allowed.";
}
}
$p1 = $_POST['pass1'];
if(empty($p1))
{
$p1Err = "* Password is required";
}
else
{
if(strlen($p1)<8)
$p1Err = "* Length of password must be minimum of 8 characters";
}
$p2 = $_POST['pass2'];
if(empty($p2))
{
$p2Err = "* Retype password is required";
}
1
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
else
{
if($p1!=$p2)
$p2Err = "* Password not match. Try again.";
}
$mb = $_POST['mob'];
if(empty($mb))
{
$mbErr = "* Mobile number is required";
}
else
{
if (!preg_match ("/^[0-9]*$/", $mb) )
{
$mbErr = "* Only numeric value is allowed.";
}
else
{
if (strlen($mb) != 10)
{
$mbErr = "* Mobile no. must contain 10 digits.";
}
}
}
if(isset($_POST['gender']))
$gender = $_POST['gender'];
if(empty($gender))
{
$genderErr = "* Subject must have to select";
}
if(isset($_POST['hb1'])) { $hb1 =
$_POST['hb1'];
$hb = "$hb1";
}
if(isset($_POST['hb2'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb2 = $_POST['hb2'];
$hb .= "$hb2";
}
if(isset($_POST['hb3'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb3 = $_POST['hb3'];
$hb .= "$hb3";
}
if(isset($_POST['hb4'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb4 = $_POST['hb4'];
$hb .= "$hb4";
}
if($hb=="") {
$hbErr = "* Must have to select at least one hobby";
}
$ct = $_POST['city'];
if(empty($ct) or $ct=='select')
{

2
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
$ctErr = "* Please select any one city";
}
$ad = $_POST['addr'];
if(empty($ad))
{
$adErr = "* Address is required.";
}
else
{
if(!preg_match('/^[A-Za-z0-9\-,.\s]+$/',$ad))
$adErr = "* special charaters not allow";
}
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid Email Format! <br>";
}

if(isset($_FILES['image'])){
$file_ext=explode('.',$_FILES['image']['name']);
$file_ext=end($file_ext);
$file_ext=strtolower($file_ext);

$image = $email.".".$file_ext;

if($file_ext!="jpeg" and $file_ext!="jpg" and $file_ext!="png"){


$imageErr="extension not allowed, please choose a JPG or JPEG or PNG file.";
}

if($_FILES['image']['size'] > 102400) {


$imageErr='File size must be not more than 100 KB';
}
}

if($nmErr=="" and $emailErr=="" and $p1Err=="" and $p2Err=="" and $mbErr=="" and $genderErr=="" and
$hbErr=="" and $ctErr=="" and $adErr=="" and $imageErr=="" and isset($_POST['btnsub']))
{
move_uploaded_file($_FILES['image']['tmp_name'],"images/$image");
$query_str =
"btnsub=Submit&nam=$nm&pass1=$p1&mob=$mb&gender=$gender&city=$ct&addr=$ad&email=$email
&hb=$hb&image=$image";
header("location:regdatastore.php?$query_str");
}
}

?>
<form action="" method="POST" enctype="multipart/form-data">
<table border="5px" style="margin:auto;border-collapse:collapse;">
<tr>
<td></td>
<td><b>Registration Page</b></td>
<td></td>
</tr>
<tr>
<td>Enter Name:</td>
<td><input type="text" name="nam" id="nam" value="<?php echo $nm; ?>"></td>
<td><?php echo $nmErr;?></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="password" name="pass1" id="pass1" value=<?php echo $p1;?>></td>

3
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<td><?php echo $p1Err;?></td>
</tr>
<tr>
<td>Retype Password:</td>
<td><input type="password" name="pass2" id="pass2" value=<?php echo $p2;?>></td>
<td><?php echo $p2Err;?></td>
</tr>
<tr>
<td>Enter Mobile:</td>
<td><input type="text" name="mob" id="mob" value=<?php echo $mb;?>></td>
<td><?php echo $mbErr;?></td>
</tr>
<tr>
<td>Select Hobbies:</td>
<td>
<input type="checkbox" name="hb1" id="hb1" value="Travelling" <?php if($hb1!="") echo "checked"
?>>Travelling
<input type="checkbox" name="hb2" id="hb2" value="Sports" <?php if($hb2!="") echo "checked" ?>>Sports
<input type="checkbox" name="hb3" id="hb3" value="Computers"
<?php if($hb3!="") echo "checked" ?>>Computers
<input type="checkbox" name="hb4" id="hb4" value="Arts & Music"
<?php if($hb4!="") echo "checked" ?>>Arts & Music
</td>
<td><?php echo $hbErr;?></td>
</tr>
<tr>
<td>Select Gender:</td>
<td>
<input type="radio" name="gender" id="gender" value="Male" <?php if($gender=='Male') echo "checked" ?>>Male
<input type="radio" name="gender" id="gender" value="Female"
<?php if($gender=='Female') echo "checked" ?>>Female
</td>
<td><?php echo $genderErr;?></td>
</tr>
<tr>
<td>City:</td>
<td>
<select name="city" id="city">
<option value="select" <?php if($ct=="select")echo "selected";
?>>---select---</option>
<option value="surat" <?php if($ct=='surat') echo "selected";
?>>surat</option>
<option value="ahmedabad" <?php if($ct=='ahmedabad') echo
"selected"; ?>>ahmedabad</option>
<option value="baroda" <?php if($ct=='baroda') echo
"selected"; ?>>baroda</option>
</select>
</td>
<td><?php echo $ctErr;?></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="addr" id="addr"><?php echo $ad;?></textarea></td>
<td><?php echo $adErr;?></td>
<tr>
<tr>
<td>Enter Email Id:</td>
<td><input type="email" name="email" id="email" value="<?php echo $email; ?>"></td>
<td><?php echo $emailErr;?></td>
</tr>

4
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<tr>
<td>Photo Upload:</td>
<td><input type="file" name="image" id="image"></td>
<td><?php echo $imageErr;?></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub"></td>
<td></td>
<tr>
</table>
</form>
</body>
</html>

REGISTRATIONDATASTORE :
<?php
$nm=$p1=$mb=$gender=$hb=$h4=$ct=$ad=$email=$image=""; if(isset($_GET['btnsub']))
{
$nm = $_GET['nam'];
$p1 = $_GET['pass1'];
$mb = $_GET['mob'];
$hb = $_GET['hb'];
$gender = $_GET['gender'];
$ct = $_GET['city'];
$ad = $_GET['addr'];
$email = $_GET['email'];
$image = $_GET['image'];
$conn = mysqli_connect("localhost","root","","db1");
$sql = "insert into REGISTRATION
(name,pass,mobile,hobbies,gender,city,address,email,image)
values('$nm','$p1','$mb','$hb','$gender','$ct','$ad','$email','$image')"; try {
mysqli_query($conn,$sql);
echo "<center>Data inserted successfully</center>";
}
catch(Exception $e) { if
($e->getCode() == 1062)
echo '<center>Your email is already registered plz try another email address or login</center>';
}
echo "<cener><form action='loginpage.php'>";
echo "<center><input type='submit' name='btnsub' value='Go to Login page'></center>"; echo
"</form></center>";
}
?>

5
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

LOGINPAGE :

<html>
<body>
<?php
$p1=$email="";
$p1Err=$emailErr="";

if(isset($_POST['btnsub']))
{
$p1 = $_POST['pass1'];
if(empty($p1))
{
$p1Err = "* Password is required";
}
else
{
if(strlen($p1)<8)
$p1Err = "* Length of password must be minimum of 8 characters";
}
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid Email Format! <br>";
}
$conn = mysqli_connect("localhost","root","","db1");
$sql = "select * from REGISTRATION"; $res =
mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($res)) {
if ($row['email']==$email) {
$emailErr = "";
if($row['pass']==$p1) {
$query_str = "btnsub=Submit &email=$email";
$_SESSION['email']=$email;
$_SESSION['pass']=$p1;
header("location:userhomepage.php?$query_str");
}
else{
$p1Err = "* Wrong password plz try again";
}
break;
}
else {
$emailErr = "* Email not found.";
}
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<table border="5px" style="margin:auto;border-collapse:collapse;">
<tr>
<td></td>
<td><b>Login Page</b></td>
<td></td>
</tr>
<tr>
<td>Enter Email Id:</td>
<td><input type="email" name="email" id="email" value="<?php echo $email; ?>"></td>
<td><?php echo $emailErr;?></td>
</tr>
6
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<tr>
<td>Enter Password:</td>
<td><input type="password" name="pass1" id="pass1" value=<?php echo $p1;?>></td>
<td><?php echo $p1Err;?></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub" value = "Login"></td>
<td></td>
<tr>
</table>
</form>
</body>
</html>

ADMINLOGINPAGE :
<html>
<body>
<?php

$p1=$email="";
$p1Err=$emailErr="";

if(isset($_POST['btnsub']))
{

$p1 = $_POST['pass1'];
if(empty($p1))
{
$p1Err = "* Password is required";
}
else
{
if(strlen($p1)<8)
$p1Err = "* Length of password must be minimum of 8 characters";
}
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid Email Format! <br>";
}
$conn = mysqli_connect("localhost","root","","db1");
$sql = "select * from REGISTRATION"; $res =
mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($res)) {
if ($row['email']==$email) {
$emailErr = "";
if($row['pass']==$p1) {
$_SESSION['email']=$email;
$_SESSION['pass']=$p1;
header("location:adminhomepage.php?btnsub=Submit");
}
else{
$p1Err = "* Wrong password plz try again";

7
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
}
break;
}
else {
$emailErr = "* Email not found.";
}
}
}

?>
<form action="" method="POST" enctype="multipart/form-data">
<table border="5px" style="margin:auto;border-collapse:collapse;">
<tr>
<td></td>
<td><b>Admin Login Page</b></td>
<td></td>
</tr>
<tr>
<td>Enter Email Id:</td>
<td><input type="email" name="email" id="email" value="<?php echo $email; ?>"></td>
<td><?php echo $emailErr;?></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="password" name="pass1" id="pass1" value=<?php echo $p1;?>></td>
<td><?php echo $p1Err;?></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub"></td>
<td></td>
<tr>
</table>
</form>
</body>
</html>

USERHOMEPAGE :
<?php
$nm=$p1=$mb=$gender=$hb=$h4=$ct=$ad=$email=$image=$query=""; if(isset($_GET['btnsub']))
{
$email = $_GET['email'];
$conn = mysqli_connect("localhost","root","","db1");
$sql = "select * from REGISTRATION";
$res = mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($res)) { if
($row['email']==$email) {
$nm = $row['name'];
$p1 = $row['pass'];
$mb = $row['mobile'];
$hb = $row['hobbies'];
$gender = $row['gender'];
$ct = $row['city'];
$ad = $row['address'];
8
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
$email = $row['email'];
$image = $row['image'];
break;
}
}
}
?>
<table cellpadding=5 border="5px" style="margin:auto;border-collapse:collapse;">
<tr><td>Name :</td><td><?php echo $nm ?></td></tr>
<tr><td>Password :</td><td><?php echo $p1 ?></td></tr>
<tr><td>Mobile :</td><td><?php echo $mb ?></td></tr>
<tr><td>Hobbies :</td><td><?php echo $hb ?></td></tr>
<tr><td>Gender :</td><td><?php echo $gender ?></td></tr>
<tr><td>City :</td><td><?php echo $ad ?></td></tr>
<tr><td>Address :</td><td><?php echo $ct ?></td></tr>
<tr><td>Email Id :</td><td><?php echo $email ?></td></tr>
<tr><td>Photo Upload :</td><td><img src="./images/<?php echo $image ?>" alt="comming soon" width="100"
height="100"></td></tr>
<tr>
<td><center><a href="editpage.php?email=<?php echo $email; ?>" target="_blank">Edit Data</a></center></td>
<td><center><a href="loginpage.php">Logout</a></center></center></td>
</tr>
</table>

SEARCH PAGE :
<html>
<body>

<?php
$conn = mysqli_connect("localhost","root","","db1");
$response = "";
$response .= "<table border=1>";

$str = $_GET['q'];

$sql = "select * from REGISTRATION where


name like '%$str%' or pass
like '%$str%' or mobile like
'%$str%' or hobbies like
'%$str%' or gender like
'%$str%' or city like '%$str%'
or email like '%$str%' or
address like '%$str%'";

$res = mysqli_query($conn,$sql);

while($row=mysqli_fetch_array($res))
{
$response .= "<tr>";
for($i=0;$i<10;$i++)
{
$response .= "<td>$row[$i]</td>";
}
$response .= "</tr>";
}
$response .= "</table>"; echo
$response;
?>

</body>
</html>

9
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

EDITPAGE :

<html>
<body>
<?php

$nm=$p1=$mb=$gender=$hb1=$hb2=$hb3=$hb4=$hb=$ct=$ad=$email=$image="";
$nmErr=$p1Err=$mbErr=$genderErr=$hbErr=$ctErr=$adErr=$imageErr="";

$email = $_GET['email'];
$conn = mysqli_connect("localhost","root","","db1");
$sql = "select * from REGISTRATION where email='$email'";
$res = mysqli_query($conn,$sql);

while($row=mysqli_fetch_assoc($res)) { if ($row['email']==$email) {
$nm = $row['name'];
$p1 = $row['pass'];
$mb = $row['mobile'];
$hb = $row['hobbies'];
$gender = $row['gender'];
$ct = $row['city'];
$ad = $row['address'];
$email = $row['email']; $image = $row['image'];
break;
}
}

$hobbies = explode (" ,", $hb); if(in_array("Travelling", $hobbies)) {


$hb1 = "Travelling";
}
if(in_array("Sports", $hobbies)) {
$hb2 = "Sports";
} if(in_array("Computers", $hobbies)) {
$hb3 = "Computers";
}
if(in_array("Arts & Music", $hobbies)) {
$hb4 = "Arts & Music";
}

if(isset($_GET['btnsub']))
{
$nm = $_GET['nam']; if(empty($nm))
{
$nmErr = "* Name is required";
}
else
{
if (!preg_match ("/^[a-zA-Z\s]*$/", $nm) )
{
$nmErr = "Only alphabat charaters is allowed.";
}
}
$p1 = $_GET['pass1'];
if(empty($p1))
{
$p1Err = "* Password is required";
}
else
{
if(strlen($p1)<8)

10
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
$p1Err = "* Length of password must be minimum of 8 characters";
}
$mb = $_GET['mob'];
if(empty($mb))
{
$mbErr = "* Mobile number is required";
}
else
{
if (!preg_match ("/^[0-9]*$/", $mb) )
{
$mbErr = "* Only numeric value is allowed.";
}
else
{
if (strlen($mb) != 10)
{
$mbErr = "* Mobile no. must contain 10 digits.";
}
}
}
if(isset($_GET['gender'])) $gender = $_GET['gender'];
if(empty($gender))
{
$genderErr = "* Subject must have to select";
}
if(isset($_GET['hb1'])) { $hb1 = $_GET['hb1'];
$hb = "$hb1";
}
if(isset($_GET['hb2'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb2 = $_GET['hb2'];
$hb .= "$hb2";
}
if(isset($_GET['hb3'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb3 = $_GET['hb3'];
$hb .= "$hb3";
}
if(isset($_GET['hb4'])) {
if ($hb!="") {
$hb .= " ,";
}
$hb4 = $_GET['hb4'];
$hb .= "$hb4";
}
if($hb=="") {
$hbErr = "* Must have to select at least one hobby";
}
$ct = $_GET['city'];
if(empty($ct) or $ct=='select')
{
$ctErr = "* Please select any one city";
}
$ad = $_GET['addr'];
if(empty($ad))
{

11
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
$adErr = "* Address is required.";
}
else
{
if(!preg_match('/^[A-Za-z0-9\-,.\s]+$/',$ad)) $adErr = "* special charaters not allow";
}

if(isset($_FILES['image'])){
$file_ext=explode('.',$_FILES['image']['name']);
$file_ext=end($file_ext);
$file_ext=strtolower($file_ext);

$image = $email.".".$file_ext;

if($file_ext!="jpeg" and $file_ext!="jpg" and $file_ext!="png"){


$imageErr="extension not allowed, please choose a JPG or JPEG or PNG file.";
}

if($_FILES['image']['size'] > 102400) {


$imageErr='File size must be not more than 100 KB';
}
}

if($nmErr=="" and $p1Err=="" and $mbErr=="" and $genderErr=="" and $hbErr=="" and $ctErr=="" and $adErr==""
and $imageErr=="" and isset($_GET['btnsub']))
{
$sql = "update REGISTRATION set name = '$nm',
pass = '$p1',
mobile = '$mb',
hobbies = '$hb',

city = '$ct',
address = '$ad',
image = '$image'
where email = '$email'";
mysqli_query($conn,$sql);
echo "<center>record updated successfully</center>";

?> }
<form action="" method="GET" enctype="multipart/form-data">
<table border="5px" style="margin:auto;border-collapse:collapse;">
<tr>
<td></td>
<td><big><b>Edit Data Page</b></big></td>
<td></td>
</tr>
<tr>
<td>Enter Name:</td>
<td><input type="text" name="nam" id="nam" value="<?php echo $nm; ?>"></td>
<td><?php echo $nmErr;?></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="password" name="pass1" id="pass1" value=<?php echo $p1;?>></td>
<td><?php echo $p1Err;?></td>
</tr>
<tr>
<td>Enter Mobile:</td>
<td><input type="text" name="mob" id="mob" value=<?php echo $mb;?>></td>

12
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<td><?php echo $mbErr;?></td>
</tr> <tr>
<td>Select Hobbies:</td>
<td>
<input type="checkbox" name="hb1" id="hb1" value="Travelling" <?php if($hb1!="") echo "checked"
?>>Travelling
<input type="checkbox" name="hb2" id="hb2" value="Sports" <?php if($hb2!="") echo "checked" ?>>Sports
<input type="checkbox" name="hb3" id="hb3" value="Computers"
<?php if($hb3!="") echo "checked" ?>>Computers
<input type="checkbox" name="hb4" id="hb4" value="Arts & Music"
<?php if($hb4!="") echo "checked" ?>>Arts & Music
</td>
<td><?php echo $hbErr;?></td>
</tr>
<tr>
<td>Select Gender:</td>
<td>
<input type="radio" name="gender" id="gender" value="Male" <?php if($gender=='Male') echo "checked" ?>>Male
<input type="radio" name="gender" id="gender" value="Female"
<?php if($gender=='Female') echo "checked" ?>>Female
</td>
<td><?php echo $genderErr;?></td>
</tr>
<tr>
<td>City:</td>
<td>
<select name="city" id="city">
<option value="select" <?php if($ct=="select")echo "selected";
?>>---select---</option>

?>>surat</option> <option value="surat" <?php if($ct=='surat') echo "selected";


<option value="ahmedabad" <?php if($ct=='ahmedabad') echo
"selected"; ?>>ahmedabad</option>
<option value="baroda" <?php if($ct=='baroda') echo
"selected"; ?>>baroda</option>
</select>
</td>
<td><?php echo $ctErr;?></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="addr" id="addr"><?php echo $ad;?></textarea></td>
<td><?php echo $adErr;?></td>
<tr>
<tr>
<td>Email Id:</td>
<td><input type="email" name="email" id="email" value="<?php echo $email; ?>" readonly></td>
<td></td>
</tr>
<tr>
<td>Photo Upload:</td>
<td><img src="./images/<?php echo $image ?>" alt="comming soon" width="100" height="100"></td>
<td><?php echo $imageErr;?></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub" value="Edit"></td>
<td></td>
<tr>
</table>
</form>

13
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
</body>
</html>
SEARCHUSINGAJEX :

<html>
<head> <script>
function showResult(str){ var xmlhttp=new
XMLHttpRequest();
xmlhttp.onreadystatechange=function(){ if
(this.readyState==4 && this.status==200) {
document.getElementById("livesearch").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true); xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch">Diplay Data here</div>
</body>
</html>

ADMINHOMEPAGE :

<?php
if(isset($_GET['btnsub']))
{
$response = "";
$email="";
$conn = mysqli_connect("localhost","root","","db1");
$sql = "select * from REGISTRATION";
$res = mysqli_query($conn,$sql);
$response .= "<table border=1>";
while($row=mysqli_fetch_array($res))
{
$response .= "<tr>";
for($i=0;$i<12;$i++)
{
if($i==8) {
$email = $row[$i];
$response .= "<td>$row[$i]</td>";
}
elseif($i==9) {
$response .= "<td><img src='./images/$row[$i]' alt='comming soon' width='100'
height='100'></td>";
}
elseif($i==10) {
$response .= "<td><a href='editpage.php?email=$email' target='_blank'>Edit</a></center></td>";
}
elseif($i==11) {
$response .= "<td><a href='deletepage.php?email=$email' target='_blank'>Delete</a></center></td>";
}
else {
$response .= "<td>$row[$i]</td>";
}
}
$response .= "</tr>";
}
$response .= "</table>";
echo $response;

14
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
}
?>
<a href='searchUsingAjax.php' target='_blank'>Search</a></center>

Deletepage :

<?php
$email = $_GET['email'];
$conn = mysqli_connect("localhost","root","","db1"); $sql =
"delete from REGISTRATION where email='$email'";
mysqli_query($conn,$sql);
echo "<center>record deleted successfully</center>";
?>

Flask Website

 Flask1.py:

import os from flask import *


from flask_mysqldb import MySQL

app = Flask( name )

app.secret_key = 'any string' app.config['MYSQL_HOST'] =


'localhost' app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '' app.config['MYSQL_DB'] = 'flask'
app.config['UPLOAD_FOLDER'] = os.path.join('static','image')

mysql = MySQL(app)

@app.route('/') def hello():


return
"hello"

@app.route('/welcome') def welcome():

if 'rec_email' in session:

return redirect(url_for('user')) return


redirect(url_for('dologin'))

@app.route('/user') def user(): if


'rec_email' in session:
id=session['rec_id'] user =
session['rec_email'] name =
session['rec_name'] pas =
session['rec_pass'] mobil =
session['rec_mob'] tv =
session['rec_tra'] red = session['rec_red']
code = session['rec_cod'] dance = session['rec_dan']
gender = session['rec_gend'] cty =
session['rec_city'] addre = session['rec_add']
ph = session['rec_img'] return
render_template('show.html',id=id,user=user,name=name,pas=pas,mobil=mobil,tv=tv,red=red,code=code,dan
ce=dance,gender=gender,cty=cty,addre=addre,ph=ph) @app.route('/drop') def droptbl(): cursor =

15
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
mysql.connection.cursor() cursor.execute("drop table registration") mysql.connection.commit() cursor.close()
return "sucessfully drop table"

@app.route('/create') def createtable():


cursor=mysql.connection.cursor() cursor.execute("create table registration (id int primary key
auto_increment,name text(30),password text(20),retype text(20),mobile int(10),travelling int(1),reading int(1),coding
int(1),dancing int(1),gender text(10),city text(30),address text(50),email text(30),photo text)")
mysql.connection.commit() cursor.close() return "table
created sucessfully"

@app.route('/registration') def registration():


return render_template('registration.html')

@app.route('/reg', methods=['POST']) def reg():

if request.method == 'POST': nm =
request.form['name'] ps =
request.form['pass'] rps =
request.form['retype'] mb =
request.form['mobile'] if
request.form.get('h1'):
h1 = b'1' else: h1 = b'0' if
request.form.get('h2'):
h2 = b'1' else: h2 = b'0' if
request.form.get('h3'):
h3 = b'1' else: h3 = b'0' if
request.form.get('h4'):
h4 = b'1' else:
h4 = b'0' gd =
request.form['g1'] ci = request.form['city']
ad = request.form['add'] em = request.form['email']
ph = request.files['file']

cursor = mysql.connection.cursor() cursor.execute("insert into


registration(name,password,retype,mobile,travelling,reading,coding,dancing,gender,city,address,email,photo) values
('{}','{}','{}','{}',{},{},{},{},'{}','{}','{}','{}','{}')".format(nm,ps,rps,mb,h1,h2,h3,h4,gd,ci,ad,em,ph.filename))
ph.save(os.path.join(app.config['UPLOAD_FOLDER'],ph.filename)) mysql.connection.commit() cursor.close()
return redirect('login')

@app.route('/verify',methods = ['POST']) def verify():

if request.method == 'POST' and 'email' in request.form and 'pass' in request.form:


username = request.form['email'] password =
request.form['pass'] cursor
= mysql.connection.cursor() cursor.execute("select * from registration where email = %s and password
= %s",(username,password))
rec = cursor.fetchone() if rec :
session['rec_id'] = rec[0] session['rec_name'] = rec[1]
session['rec_pass'] = rec[2] session['rec_mob'] = rec[4]
session['rec_tra'] = rec[5] session['rec_red'] = rec[6]
session['rec_cod'] = rec[7] session['rec_dan'] = rec[8]
session['rec_gend'] = rec[9] session['rec_city'] =
rec[10] session['rec_add'] = rec[11]
session['rec_email'] = rec[12] session['rec_img'] =
rec[13] return redirect(url_for('welcome')) else:
flash("Invalid..! Please try again..!") return
redirect(url_for('dologin'))

@app.route('/login') def dologin():

return render_template('login.html')

16
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
@app.route('/ad-login',methods=['POST','GET']) def ad_login(): if(request.method=='POST' and 'ad' in
request.form and 'ps' in request.form):
admin=request.form['ad'] pas=request.form['ps']
if(admin=='admin' and pas=='admin123'):
session['admin']=admin return
redirect(url_for('show')) return
"<script>alert('invalid !!')</script>" return
render_template('admin_login.html')

@app.route('/show') def show():


if ('admin' in session):
cursor = mysql.connection.cursor() cursor.execute("select * from
registration")
mysql.connection.commit() data = cursor.fetchall() if data:
return render_template('home.html',data=data) return
redirect(url_for('ad_login'))

@app.route('/edit/<id>',methods=['POST','GET']) def edit(id): if(id):


cursor = mysql.connection.cursor()
cursor.execute("select * from registration where id={}".format(id)) user = cursor.fetchone()
if(request.method=='POST' and 'nm' in request.form and 'ps' in request.form and 'rps' in request.form and 'mb' in request.form
and 'ci' in request.form and 'ad' in request.form and 'em' in request.form ): nm = request.form['name'] ps =
request.form['pass'] rps = request.form['retype'] mb = request.form['mobile'] if request. form.get('h1'):
h1 = b'1' else: h1 = b'0' if request.form.get('h2'):
h2 = b'1' else:
h2 = b'0' if
request.form.get('h3'): h3 = b'1'
else:
h3 = b'0' if request.form.get('h4'):
h4 = b'1' else:
h4 = b'0' gd = request.form['g1']
ci = request.form['city'] ad = request.form['add']
em = request.form['email'] ph =
request.files['file'] cursor=mysql.connection.cursor()
if(ph):
ph.save(os.path.join(app.config['UPLOAD_FOLDER'],ph.filename)) cursor.execute("update registration set
name='{}',password='{}',retype='{}',mobile='{}',travelling={},reading={},coding={},dancing={},gender='{}
'
,city='{}',address='{}',email='{}',photo='{}' where
id={}".format(nm,ps,rps,mb,h1,h2,h3,h4,gd,ci,ad,em,ph.filename,id)) mysql.connection.commit()
cursor.close() if('admin' in session):
return redirect(url_for('welcome'))
return redirect(url_for('dologin')) else: cursor.execute("update registration set
name='{}',password='{}',retype='{}',mobile='{}',travelling={},reading={},coding={},dancing={},gender='{}
'
,city='{}',address='{}',email='{}' where id={}".format(nm,ps,rps,mb,h1,h2,h3,h4,gd,ci,ad,em,id))
mysql.connection.commit()
cursor.close() if('admin' in session):
return redirect(url_for('welcome'))
return redirect(url_for('dologin')) return render_template('edit.html',user=user) return
redirect(url_for('dologin'))

@app.route('/logout') def logout(): session.pop('rec_id',None)


session.pop('rec_email',None) return redirect(url_for('dologin'))

@app.route('/delete/<id>') def delete(id):


cursor = mysql.connection.cursor()
cursor.execute("delete from registration where id={}".format(id)) mysql.connection.commit()
cursor.close() return "delete successfully" + "<br><a href='/show'>Show</a>"
if name ==' main ':
app.run(debug = True)
17
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

 registration.html:

<html>
<header align="center">

<a href="ad-login">Admin</a>
<a href="login">Login</a>
<a href="registration">Registration</a>
<a href="show">Show</a>
<script> function
validate(){
if(myForm.name.value == ""){
alert("Please provide your Name..!") myForm.name.focus()
return false
}
if(!isNaN(myForm.name.value)){ alert("Please Enter only
character") myForm.name.focus() return false
}
if(myForm.pass.value == ""){
alert("Please fill up Password..!") myForm.pass.focus() return false
}

if(myForm.retype.value == ""){ alert("Please Fill up Retype


Password..!")
myForm.retype.focus()
return false
}
var pa1 = myForm.pass.value var pa2 =
myForm.retype.valueif(pa1
!= pa2){ alert("Not
match password..!") myForm.retype.focus()
return false
}
if(myForm.mobile.value == ""){
alert("Please Enter your Mobile number..!")
myForm.mobile.focus()
return false
}
var gender = myForm.g1.value
if(gender.checked == false){ alert("please select
gender")
return false
}

if(myForm.city.value == "" || myForm.city.value == 'select'){


alert("Please select any one city..!")
myForm.city.focus() return false
}
if(myForm.add.value == ""){ alert("Please Enter Address..!")
myForm.add.focus() return false
}
if(myForm.email.value == ""){
alert("Please Enter Email..!") myForm.email.focus()
return false
}
var mail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ if(!myForm.email.value.match(mail)){
alert("invalid") return false
}
if(myForm.file.value == ""){ alert("Please upload image..!")
myForm.file.focus()

18
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
return false
}

}
</script>
</header>
<body style="background-color: rgb(170, 170, 177);">
<h2 align="center" style="color: rgb(5, 5, 75);">Registration</h2>

<form action="/reg" method="post" name="myForm" onsubmit="return(validate())" enctype="multipart/form-data">


<table border="3" align="center" style="background-color: white;">
<tr>
<td>Name :</td>
<td><input type="text" name="name" placeholder="Enter name" ></td>

</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="pass" placeholder="Password" pattern="(?=.*\d)(?=.*[az])(?=.*[A-Z]).{6,}"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="Must contain at least one number and one uppercase and lowercase
letter and at least 6 or more character.."></td>
</tr>
<tr>
<td>Retype Password :</td>
<td><input type="password" name="retype" placeholder="Retype Password"></td>
</tr>
<tr>
<td>Mobile Number :</td>
<td><input type="number" name="mobile" placeholder="Enter Mobile number"></td>
</tr>
<tr>
<td>Hobbies :</td>
<td><input type="checkbox" name="h1" value="1" >Travelling
<input type="checkbox" name="h2" value="1">Reading
<input type="checkbox" name="h3" value="1">Coding
<input type="checkbox" name="h4" value="1">Dancing</td>
</tr>
<tr>
<td>Gender :</td>
<td><input type="radio" name="g1" value="Male">Male
<input type="radio" name="g1" value="Female">Female</td>
</tr>
<tr>
<td>City :</td>
<td>
<select name="city">
<option value="select" selected>---select---</option>
<option value="surat" >Surat</option>
<option value="ahemdabad" >Ahemdabad</option>
<option value="baroda">Baroda</option>
<option value="banglore">Banglore</option>
</select>
</td>
</tr>
<tr>
<td>Home Address :</td>
<td><textarea type="text" name="add" placeholder="Address"></textarea></td>
</tr>
<tr>
<td>
Email id :

19
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
</td>
<td><input name="email" name="email" placeholder="Email id"></td>
</tr>
<tr>
<td>
Photo :
</td>
<td><input type="file" name="file" ></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub" style="color: green;"></td>
</tr>
</table>
</form>

</body>
</html>

 Login.html:

<html>
<header align="center">

<a href="ad-login">Admin</a>
<a href="login">Login</a>
<a href="registration">Registration</a>
<a href="show">Show</a>
<script>
function validate(){ if(document.myForm.email.value ==
""){ alert("Please provide your Email..!")
document.myForm.email.focus() return false
}
if(document.myForm.pass.value == ""){ alert("Please fill up Password..!")
document.myForm.pass.focus() return false
}
}
</script>
</header>
<body style="background-color: rgb(179, 179, 182);" align="center">
<h2 align="center" style="color: rgb(7, 7, 102);">Login </h2>

<form action="/verify" method="post" name="myForm" onsubmit="return(validate())">

<table border="3" align="center" style="background-color: white;">


<tr>
<td>
Email Id :
</td>
<td>
<input type="email" name="email" >
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
<input type="password" name="pass" >
</td>

20
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="btnsub" style="color: green;">
</td>
</tr>
</table>
<br>
{% if get_flashed_messages() %}
{{ get_flashed_messages()[0] }}
{% endif %}

</form>
</body>
</html>

 Show.html:
<html>
<head>

<a href="login">Login</a>
<a href="registration">Registration</a>
</head>
<body align="center" style="background-color: rgb(177, 176, 176);">
<h2 style="color: green;">Profile</h2>

<form>

<table border="3" align="center" style="background-color: white;">


<tr>
<td>
Photo :</td>
<td><imgsrc="http://127.0.0.1:5000/static/image/{{ph}}" alt="profile" width="120" height="120"></td>
</tr>
<tr>
<td>ID :</td>
<td>{{id}}</td>

</tr>
<tr>
<td>Name :</td>
<td>{{name}}</td>

</tr>
<tr>
<td>Password :</td>
<td>{{pas}}</td>
</tr>
<tr>
<td>Mobile Number :</td>
<td>{{mobil}}</td>
</tr>
<tr>
<td>Travelling :</td>
<td style="color: blue;">{{tv}}</td>
</tr>
<tr>
<td>Reading :</td>
<td style="color: blue;">{{red}}</td>
</tr>
21
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<tr>
<td>Coding :</td>
<td style="color: blue;">{{code}}</td>
</tr>
<tr>
<td>Dancing :</td>
<td style="color: blue;">{{dance}}</td>
</tr>
<tr>
<td>Gender :</td>
<td>{{gender}}</td>
</tr>
<tr>
<td>City :</td>
<td>
{{cty}}
</td>
</tr>
<tr>
<td>Home Address :</td>
<td>{{addre}} </td>
</tr>
<tr>
<td> Email id :
</td>
<td>{{user}}</td>
</tr>

</table>
</form>
<a href="/logout">Logout</a>
<a href="{{url_for('edit',id=id)}}">Edit</a></body>
</html>

 Home.html:

<html>
<body style="background-color: white;" align="center">
<header align="center">

<a href="login">Login</a>
<a href="registration">Registration</a>
<a href="show">Show</a>
</header>
<h2 align="center">Home page</h2>

<table align="center" border="2">


<tr>
<th>ID</th>
<th>Name</th>
<th>Password</th>
<th>Retype</th>
<th>Mobile</th>
<th>Travelling</th>
<th>Reading</th>
<th>Coding</th>
<th>Dancing</th>
<th>Gender</th>
<th>City</th>
22
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<th>Address</th>
<th>Email id</th>
<th>Photo</th>
<th>Edit</th>
<th>Delete</th>
</tr><br>
{% for row in data %}

<tr>
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
<td>{{row[4]}}</td>
<td>{{row[5]}}</td>
<td>{{row[6]}}</td><td>{{row[7]}}</td>
<td>{{row[8]}}</td>
<td>{{row[9]}}</td>
<td>{{row[10]}}</td>
<td>{{row[11]}}</td>
<td>{{row[12]}}</td>
<td><imgsrc="http://127.0.0.1:5000/static/image/{{row[13]}}" alt="profile" width="100" height="100"></td>
<td><a href="{{url_for('edit',id=row[0])}}">Edit</a></td>
<td><a href="{{url_for('delete',id=row[0])}}">Delete</a></td>
</tr>
{% endfor %}
</table>
<br>
<a href="/logout">Logout</a></body></html>

 Edit.html:

<html>
<header align="center">

<script>
function validate(){
if(myForm.name.value == ""){
alert("Please provide your Name..!") myForm.name.focus()
return false
}
if(!isNaN(myForm.name.value)){
alert("Please Enter only character") myForm.name.focus() return
false
}
if(myForm.pass.value == ""){ alert("Please fill up
Password..!")
myForm.pass.focus() return false
}

if(myForm.retype.value == ""){
alert("Please Fill up Retype Password..!")
myForm.retype.focus()
return false
}
var pa1 = myForm.pass.value var pa2 =
myForm.retype.valueif(pa1
!= pa2){ alert("Not
match password..!") myForm.retype.focus()
return false

23
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
}
if(myForm.mobile.value == ""){
alert("Please Enter your Mobile number..!") myForm.mobile.focus() return
false
}
var gender = myForm.g1.value
if(gender.checked == false){ alert("please select
gender")
return false
}

if(myForm.city.value == "" || myForm.city.value == 'select'){


alert("Please select any one city..!")
myForm.city.focus() return false
}
if(myForm.add.value == ""){ alert("Please Enter
Address..!") myForm.add.focus() return
false
}
if(myForm.email.value == ""){
alert("Please Enter Email..!") myForm.email.focus()
return false
}
var mail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
if(!myForm.email.value.match(mail)){ alert("invalid") return false
}
if(myForm.file.value == ""){
alert("Please upload image..!")
myForm.file.focus()
return false
}

}
</script>
</header>
<body style="background-color: rgb(170, 170, 177);">
<h2 align="center" style="color: rgb(5, 5, 75);">Registration</h2>

<form action="{{url_for('edit',id=user[0])}}" method="post" name="myForm" onsubmit="return(validate())"


enctype="multipart/form-data">
<table border="3" align="center" style="background-color: white;">
<tr>
<td>Name :</td>
<td><input type="text" name="name" placeholder="Enter name" value="{{user[1]}}"></td>

</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="pass" placeholder="Password" value="{{user[2]}}" pattern="(?=.*\d)(?=.*[a-
z])(?=.*[A-Z]).{6,}" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="Must contain at least one number and one
uppercase and lowercase letter and at least 6 or more character.."></td>
</tr>
<tr>
<td>Retype Password :</td>
<td><input type="password" name="retype" placeholder="Retype Password" value="{{user[3]}}"></td>
</tr>
<tr>
<td>Mobile Number :</td>
<td><input type="number" name="mobile" placeholder="Enter Mobile number" value="{{user[4]}}"></td>
</tr>
<tr>

24
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
<td>Hobbies :</td>
<td><input type="checkbox" name="h1" value="1" {% if user[5]==1 %}{{"checked" }}{% endif %}>Travelling
<input type="checkbox" name="h2" value="1" {% if user[6]==1 %}{{"checked"}}{% endif %}>Reading
<input type="checkbox" name="h3" value="1" {% if user[7]==1 %}{{"checked"}}{% endif %}>Coding
<input type="checkbox" name="h4" value="1" {% if user[8]==1 %}{{"checked"}}{% endif %}>Dancing</td>
</tr>
<tr>
<td>Gender :</td>
<td><input type="radio" name="g1" value="Male" {% if user[9]=='Male' %}{{"checked"}}{% endif %}>Male
<input type="radio" name="g1" value="Female" {% if user[9]=='Female' %}{{"checked"}}{% endif %}>Female</td>
</tr>
<tr>
<td>City :</td>
<td>
<select name="city">
<option value="select" selected>---select---</option>
<option value="surat" {% if user[10]=='surat' %}{{"selected"}}{% endif %}>Surat</option>
<option value="ahemdabad" {% if user[10]=='ahemdabad' %}{{"selected"}}{% endif %}>Ahemdabad</option>
<option value="baroda" {% if user[10]=='baroda' %}{{"selected"}}{% endif %}>Baroda</option>
<option value="banglore" {% if user[10]=='banglore' %}{{"selected"}}{% endif %}>Banglore</option>
</select>
</td>
</tr>
<tr>
<td>Home Address :</td>
<td><textarea type="text" name="add" placeholder="Address" value="{{user[11]}}">{{user[11]}}</textarea></td>
</tr>
<tr>
<td>
Email id :
</td>
<td><input name="email" name="email" placeholder="Email id" value="{{user[12]}}"></td>
</tr>
<tr>
<td>
Photo :
</td>
<td><input type="file" name="file" value="{{user[13]}}"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnsub" value="update" style="color: green;"></td>
</tr>
</table>
</form>

</body>
</html>

 Admin_login.html:

<html>
<header align="center">

<a href="login">Login</a>
<a href="registration">Registration</a>
<a href="show">Show</a>
<script> function validate(){
if(document.myForm.ad.value == ""){
alert("Please provide your Email..!")
25
TYBCA-SEM-5-DIV-2 PHP Roll no. -108
document.myForm.ad.focus() return false
}
if(document.myForm.ps.value == ""){ alert("Please fill up
Password..!")
document.myForm.ps.focus() return false
}
}
</script>
</header>
<body style="background-color: rgb(179, 179, 182);" align="center">
<h2 align="center" style="color: rgb(7, 7, 102);">Login </h2>

<form action="/ad-login" method="post" name="myForm" onsubmit="return(validate())">

<table border="3" align="center" style="background-color: white;">


<tr>
<td>
Admin :
</td>
<td>
<input type="text" name="ad" >
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
<input type="password" name="ps" >
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="btnsub" value="Login" style="color: green;">
</td>
</tr>
</table>
<br>
{% if get_flashed_messages() %}
{{ get_flashed_messages()[0] }}
{% endif %}

</form></body></html>

 Output:

26
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

27
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

28
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

29
TYBCA-SEM-5-DIV-2 PHP Roll no. -108

30

You might also like