In PHP, CRUD means C(Create), R(Read), U(Update), D(Delete). Here we will learn…

  • How to build a connection between a PHP file with MySQL(Open-Source,Relational) Database.
  • How to insert record into a MySQL table using HTML form + PHP
  • How to update record into any MySQL table using PHP
  • How to delete record from a MySQL table using PHP.
  • How to view all records inside any MySQL table.

We will also learn how to deal with BLOB (ex. images) objects using PHP, how to insert into MySQL table, how to view that etc.

We will consider a common database – MySQL

Software’s – windows 10, XAMPP server, VS code.

Configuration –

Database name – microcodes
User-name – root
Password – NULL
Host – localhost/192.168.56.1

remember: if you use another system like Linux, then your configuration nay changed!!

Q. How to install LAMP stack in Linux for practice/test PHP+MySQL app inside Ubuntu Linux machine?

Ans: see our video: https://www.youtube.com/watch?v=5LZs9mv4rG0&t=42s

  • SELECT statement:

Using this statement we can view/fetch all the existing records from MySQL table.

<?php
// establishing database connection
$conn = mysqli_connect(‘localhost’,’username’,’password’,’dbname’);
 
$sql=”SELECT * FROM table_name”;
$query=mysqli_query($conn,$sql);
 
while($row=mysqli_fetch_assoc($query)){
    echo $row[‘column_name1’];
               echo “<br>”;
    echo $row[‘column_nameN’];
}
 
?>

INSERT statement:

using this statement we can insert a new record into database’s table.

<?php
error_reporting(0);
// connection with database
$conn = mysqli_connect(‘localhost’,’username’,’password’,’dbname’);
 
// getting values from .. form
$var1 = $_POST[‘input_box_1’];
$var2 = $_POST[‘ input_box_2’];
 
$sql=”INSERT INTO table_name(column1,column2,…) VALUES(‘$var1’,’$var2’,…)”;
$query = mysqli_query($conn,$sql);
 
if($query){
echo “data send successfuly!”;
}
else{
    echo “data can’t send”;
}
 
?>

UPDATE statement:  

we can update table existing records using this statement. For this we need first need any primary key or alternate key of table for uniquely identify table’s record. After that we can update table’s record.

<?php
error_reporting(0);
// connection with database
$conn = mysqli_connect(‘localhost’,’username’,’password’,’dbname’);
 
// getting values from… form
$var1 = $_POST[‘input_box_1’];
$var2 = $_POST[‘ input_box_2’];
$varN=$_POST[‘input_box_N’];
 
$sql=”UPDATE table_name SET column1=’$var1′,column2=’$var2′,column=’$varN’ WHERE condition”;
$query = mysqli_query($conn,$sql);
 
if($query){
echo “record updated successfuly!”;
}
else{
echo “record not updated!”;
}
 
?>

DELETE statement:

using this statement we can delete/remove table’s existing record.

<?php
error_reporting(0);
$conn = mysqli_connect(‘localhost’,’username’,’password’,’dbname’);
 
// getting values from .. form
$var1 = $_POST[‘input_box_1’];
$var2 = $_POST[‘ input_box_2’];
$varN = $_POST[‘input_box_N’];
 
$sql=”DELETE FROM table_name WHERE condition”;
$query = mysqli_query($conn,$sql);
 
if($query){
echo “record deleted successfuly!”;
}
else{
echo “record not deleted!”;
}
 
?>