Php Short Course Crud
Login Page
with Session
<?php
include '../dbconn.php';
session_start();
if(isset($_POST['submit'])) {
$name =$_POST['uname'];
$pass =$_POST['pass'];
$query = mysqli_query($conn,"select * from user where name='$name' And password='$pass' ");
$row =mysqli_fetch_array($query);
if(mysqli_num_rows($query)>0) {
$_SESSION['user'] = $row['id'];
echo $_SESSION['user'];
header("location:input.php");
}
}
?>
<html>
<head>
<title></title></head>
<body>
<form method="post" action="">
Username<input type="text" name="uname"><br>
Password<input type="password" name="pass"><br>
<input type="submit" name="submit" value="submit"><br>
</form>
</body>
</html>
Logout Page
<?php
session_start();
header("location:login.php");
session_destroy();
?>
Data input Page
<?php
include '../dbconn.php';
session_start();
if(isset($_SESSION['user']))
{
echo $_SESSION['user'];
$id= $_SESSION['user'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$country = $_POST['country'];
$comment = $_POST['comment'];
$query =mysqli_query($conn,"insert into cav (uid,name,phone,country,comment)
values
('$id','$name','$phone','$country','$comment') ");
?>
<html>
<head><title></title></head>
<body><h2><a href="logout.php">Logout</a></h2>
<form method="post" action="">
Name:<input type="text" name="name"><br>
Phone:<input type="number" name="phone"><br>
Country:<input type="text" name="country"><br>
Comment<input type="textarea" name="comment"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php
}
else
{
session_destroy();
header('Location:login.php');
}
?>
Show Data Page
<html>
<head><title>Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2><a href="logout.php">Logout</a></h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
<th>Country</th>
<th>Operation</th>
</tr>
<?php
include '../dbconn.php';
include '../dbconn.php';
session_start();
if(isset($_SESSION['user'])){
echo $id = $_SESSION['user'];
$query =mysqli_query($conn,"select * from cav ");
?>
<?php
while($row=mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>". $row['id'] ."</td>" ;
echo "<td>". $row['name'] ."</td>" ;
echo "<td>". $row['phone'] ."</td>" ;
echo "<td>". $row['country']."</td>";
echo "<td>"." <a href='edit.php?edit=$row[id]'>edit</a>" ."</td> ";
echo "<td>". "<a href='delete.php?delete=$row[id]'>Delete</a>". "</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
Edit Page
<?php
include '../dbconn.php';
session_start();
if(isset($_SESSION['user'])){
echo $_SESSION['user'];
$id = $_GET['edit'];
$query=mysqli_query($conn,"select * from cav where id='$id' ");
$row= mysqli_fetch_array($query);
if(isset($_POST['save'])) {
$name=$_POST['name'];
$phone=$_POST['phone'];
$country=$_POST['country'];
$comment=$_POST['comment'];
$sql ="update cav set name='$name', phone='$phone', country='$country', comment='$comment' where id='$id' ";
$results = mysqli_query($conn,$sql);
}
}
?>
<html>
<head><title></title></head>
<body>
<form action="" method="POST">
<input type="text" name="name" value="<?php echo $row['name']; ?> ">
<input type ="text" name="phone" value="<?php echo $row['phone']; ?> ">
<input type="text" name="country" value="<?php echo $row['country']; ?> ">
<input type ="textarea" name="comment" value="<?php echo $row['comment']; ?> ">
<input type="submit" name="save" value="update">
</form>
</body>
</html>
Delete Page
<?php
include '../dbconn.php';
session_start();
if(isset($_SESSION['user']))
{
echo $_SESSION['user'];
$id=$_GET['delete'];
$delete ="delete from cav where id='$id'";
$q=mysqli_query($conn,$delete);
echo "Deleted Item";
}
?>
Find Data Record
<html>
<head>
<title></title></head>
<body>
<form method="post" action="">
<input type="text" name="search" >
<input type="submit" name="find" value="find">
</form>
<!--
<hr>
<h2><a href="logout.php">Logout</a></h2>
<h2><a href="login.php">Login Page</a></h2>
<h2><a href="show.php">Show Data</a></h2>
<h2><a href="edit.php">Edit Page</a></h2>
<h2><a href="input.php">input Data</a></h2>
<hr> -->
</body>
</html>
<?php
include '../dbconn.php';
session_start();
if(isset($_SESSION['user'])){
echo $_SESSION['user'];
if(isset($_POST['find'])) {
$ser = $_POST['search'];
/*$find =mysqli_query($conn,"SELECT * FROM `cav` WHERE `id` LIKE '%$ser%' ") or die(mysqli_error());*/
$find = mysqli_query($conn,"select * from cav where country like '%$ser%'");
echo "The Record:".mysqli_num_rows($find);
$row=mysqli_fetch_array($find);
echo $row['id'];
echo $row['name'];
echo $row['phone'];
echo $row['country'];
}
}
?>
Comments
Post a Comment