Connection :
<?php
mysql_connect("localhost","root",'');
mysql_select_db("DB Name")or die(mysql_error());
echo "Welcome";
?>
------------------------------------------------------------------------------------------------------------------
1) dbcon
<?php
$con = mysqli_connect("localhost","root","","blog");
if(!$con){
die('Connection Failed'. mysqli_connect_error());
}
?>
2) code--------------
<?php
session_start();
require 'dbcon.php';
if(isset($_POST['delete_student']))
{
$student_id = mysqli_real_escape_string($con, $_POST['delete_student']);
$query = "DELETE FROM students WHERE id='$student_id' ";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$_SESSION['message'] = "Student Deleted Successfully";
header("Location: index.php");
exit(0);
}
else
{
$_SESSION['message'] = "Student Not Deleted";
header("Location: index.php");
exit(0);
}
}
if(isset($_POST['update_student']))
{
$student_id = mysqli_real_escape_string($con, $_POST['student_id']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$course = mysqli_real_escape_string($con, $_POST['course']);
$query = "UPDATE students SET name='$name', email='$email', phone='$phone', course='$course' WHERE id='$student_id' ";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$_SESSION['message'] = "Student Updated Successfully";
header("Location: index.php");
exit(0);
}
else
{
$_SESSION['message'] = "Student Not Updated";
header("Location: index.php");
exit(0);
}
}
if(isset($_POST['save_student']))
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$course = mysqli_real_escape_string($con, $_POST['course']);
$query = "INSERT INTO students (name,email,phone,course) VALUES ('$name','$email','$phone','$course')";
$query_run = mysqli_query($con, $query);
if($query_run)
{
$_SESSION['message'] = "Student Created Successfully";
header("Location: student-create.php");
exit(0);
}
else
{
$_SESSION['message'] = "Student Not Created";
header("Location: student-create.php");
exit(0);
}
}
?>
3)student-create // insert--------
<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Student Create</title>
</head>
<body>
<div class="container mt-5">
<?php include('message.php'); ?>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Student Add
<a href="index.php" class="btn btn-danger float-end">BACK</a>
</h4>
</div>
<div class="card-body">
<form action="code.php" method="POST">
<div class="mb-3">
<label>Student Name</label>
<input type="text" name="name" class="form-control">
</div>
<div class="mb-3">
<label>Student Email</label>
<input type="email" name="email" class="form-control">
</div>
<div class="mb-3">
<label>Student Phone</label>
<input type="text" name="phone" class="form-control">
</div>
<div class="mb-3">
<label>Student Course</label>
<input type="text" name="course" class="form-control">
</div>
<div class="mb-3">
<button type="submit" name="save_student" class="btn btn-primary">Save Student</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
4) student-edit -------------------------
<?php
session_start();
require 'dbcon.php';
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Student Edit</title>
</head>
<body>
<div class="container mt-5">
<?php include('message.php'); ?>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Student Edit
<a href="index.php" class="btn btn-danger float-end">BACK</a>
</h4>
</div>
<div class="card-body">
<?php
if(isset($_GET['id']))
{
$student_id = mysqli_real_escape_string($con, $_GET['id']);
$query = "SELECT * FROM students WHERE id='$student_id' ";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0)
{
$student = mysqli_fetch_array($query_run);
?>
<form action="code.php" method="POST">
<input type="hidden" name="student_id" value="<?= $student['id']; ?>">
<div class="mb-3">
<label>Student Name</label>
<input type="text" name="name" value="<?=$student['name'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Student Email</label>
<input type="email" name="email" value="<?=$student['email'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Student Phone</label>
<input type="text" name="phone" value="<?=$student['phone'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Student Course</label>
<input type="text" name="course" value="<?=$student['course'];?>" class="form-control">
</div>
<div class="mb-3">
<button type="submit" name="update_student" class="btn btn-primary">
Update Student
</button>
</div>
</form>
<?php
}
else
{
echo "<h4>No Such Id Found</h4>";
}
}
?>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
5)student- view -----------------
<?php
require 'dbcon.php';
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Student View</title>
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Student View Details
<a href="index.php" class="btn btn-danger float-end">BACK</a>
</h4>
</div>
<div class="card-body">
<?php
if(isset($_GET['id']))
{
$student_id = mysqli_real_escape_string($con, $_GET['id']);
$query = "SELECT * FROM students WHERE id='$student_id' ";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0)
{
$student = mysqli_fetch_array($query_run);
?>
<div class="mb-3">
<label>Student Name</label>
<p class="form-control">
<?=$student['name'];?>
</p>
</div>
<div class="mb-3">
<label>Student Email</label>
<p class="form-control">
<?=$student['email'];?>
</p>
</div>
<div class="mb-3">
<label>Student Phone</label>
<p class="form-control">
<?=$student['phone'];?>
</p>
</div>
<div class="mb-3">
<label>Student Course</label>
<p class="form-control">
<?=$student['course'];?>
</p>
</div>
<?php
}
else
{
echo "<h4>No Such Id Found</h4>";
}
}
?>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
6) index---------------------------------------
<?php
session_start();
require 'dbcon.php';
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Student CRUD</title>
</head>
<body>
<div class="container mt-4">
<?php include('message.php'); ?>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Student Details
<a href="student-create.php" class="btn btn-primary float-end">Add Students</a>
</h4>
</div>
<div class="card-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Student Name</th>
<th>Email</th>
<th>Phone</th>
<th>Course</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM students";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0)
{
foreach($query_run as $student)
{
?>
<tr>
<td><?= $student['id']; ?></td>
<td><?= $student['name']; ?></td>
<td><?= $student['email']; ?></td>
<td><?= $student['phone']; ?></td>
<td><?= $student['course']; ?></td>
<td>
<a href="student-view.php?id=<?= $student['id']; ?>" class="btn btn-info btn-sm">View</a>
<a href="student-edit.php?id=<?= $student['id']; ?>" class="btn btn-success btn-sm">Edit</a>
<form action="code.php" method="POST" class="d-inline">
<button type="submit" name="delete_student" value="<?=$student['id'];?>" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
<?php
}
}
else
{
echo "<h5> No Record Found </h5>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
0 Comments
Post a Comment