Starting Vocational Training From 1-May-2024 Get Detail
In this article, we will learn simple PHP & MySQL code to add, edit, delete and view data. This kind of system
is also referred to CRUD (Create, Read, Update, Delete).
Here is a step-by-step guide:
First of all, we will create a new MySQL database. Let us name the database as student_master.
Database Creation :
create database student_master;
Create a new table in database student_master. Let us name the table as student_info.
Table Creation :
create table student_info(id int primary key auto_increment,stud_name text,stud_address text);
Now, we will create a db_file.php file which contains database connection code.
Database Connectivity : (db_file.php)
$db = new PDO(mysql:host=localhost;dbname=student_master;charset=utf8mb4, root, );
HTML : add.php
<form name="" method="post" action="add_do.php">
<label>Student Name : </label>
<input type="text" name="txt_name" placeholder="Enter Student Name *" required>
<br>
<label>Student Address : </label>
<textarea name="txt_address" placeholder="Enter Student Address *" required></textarea>
<br>
<button type="submit">Submit</button>
</form>
Form action on add.php is add_do.php. It means that the submitted form data will
go to add_do.php.
PHP : add_do.php
$txt_name=$_REQUEST[txt_name];
$txt_address=$_REQUEST[txt_address];
$insertQuery=$db->query("insert into student_info(stud_name,stud_address)
values($txt_name,$txt_address)") or die("");
echo "Sucessfully Inserted !...";
header("index.php");
Data from database is fetched and displayed in show.php file.
This is our homepage.
<?php include("db_file.php"); ?>
<table border="1">
<thead>
<tr>
<th>S No</th>
<th>Student Name</th>
<th>Student Address</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php $Query=$db->query("select id,stud_name,stud_address from student_info order by id desc") or die("");
$i=0;
while($Result=$Query->fetch(PDO::FETCH_ASSOC)){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Result[stud_name]; ?></td>
<td><?php echo $Result[stud_address]; ?></td>
<td>
<a href="delete.php?id=<?php echo $Result[id]; ?>">Delete</a> |
<a href="update_record.php?id=<?php echo $Result[id]; ?>">Update</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Delete : (delete.php)
include("db_file.php");
$id=$_REQUEST[id];
$delQuery=$db->query("") or die("");
echo "Sucessfully Deleted !...";
header("index.php");
Update : update.php
<?php
include("db_file.php");
$id=$_REQUEST[id];
$query=$db->query("select id,stud_name,stud_address from student_info where id=$id") or die("");
$res=$query->fetch(PDO::FETCH_ASSOC);
?>
<form name="" method="post" action="update_do.php">
<label>Student Name : </label>
<input type="text" name="txt_name" value="<?php echo $res[stud_name]; ?>" placeholder="Enter Student Name *" required>
<input type="hidden" id="txt_hide" value="<?php echo $res[id]; ?>">
<br>
<label>Student Address : </label>
<textarea name="txt_address" placeholder="Enter Student Address *" required><?php echo $res[stud_address]; ?></textarea>
<br>
<button type="submit">Submit</button>
</form>
Update PHP : update_do.php
include("db_file.php");
$txt_hide=$_REQUEST[txt_hide];
$txt_name=$_REQUEST[txt_name];
$txt_address=$_REQUEST[txt_address];
$insertQuery=$db->query("update student_info set stud_name=$txt_name,stud_address=$txt_address where id=$txt_hide)") or die("");
echo "Sucessfully Updated !...";
header("index.php");
ohh you study mysql and programmation but hacked your website by systemdenied