PHP CRUD Operation Using MySQLi

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

PHP CRUD Operation Using MySQLi

We develop a simple core PHP CRUD Operation Using MySQLi With example source code.
Which you use to learn PHP basics very quick and easy way. Here I will show you Create, read,
update and delete operation with image uploading.
Anyone want a complete file in ZIP Check comment below.
So from here, we have started the code by creating a PHP MySQLi connection file, see the
below code and if have you any problem with these of any line you can ping me with the
comment box.

To Create a complete PHP CRUD Operation we create those required files,

1. connect.php
2. registration.php
3. Login.php
4. home.php
5. view.php
6. edit.php
7. delete.php
And if you like it give me a word on comment how’s it.
Lets Start,
Connect.php (DB Connection File)
This is the database connection file, that is included in all other files to maintain the DB
connection globally.

On mysqli_connect function first parameter is HOSTNAME, second is Database User


Name, third is for Password and the last parameter is for Database Name.
<?php
//Session is use for store value of variables and transfer it to one page to another
session_start();
 
$con= mysqli_connect("localhost","root","","test")
?>
Registration.php (PHP User register File)
This is the user registration page where we create a query for the insertion of user data into the
database. Where we use MySQLi which is the newer or extended version of MySQL.

//REGISTRATION PAGE

<?php
//This is connection file which we create on first and include using PHP include function.
include 'connect.php';

//we use isset function, where it take submit button name and check button is clicked or not, if not clicked
than code is not running
if (isset($_POST['sub']))
{
$t = $_POST['text'];
$u = $_POST['user'];
$p = $_POST['pass'];
$c = $_POST['city'];
$g = $_POST['gen'];
if ($_FILES['f1']['name'])
{

//This function is used for uploading image on the directory which you create on project root directory
move_uploaded_file($_FILES['f1']['tmp_name'], "image/" . $_FILES['f1']['name']);
$img = "image/" . $_FILES['f1']['name'];
}

//This is insertion query of mysql


$i = "insert into reg(name,username,password,city,image,gender)value('$t','$u','$p','$c','$img','$g')";

//On extend version of mysql, mysqli takes two parameters where First is connection variable and second
is query variable
mysqli_query($con, $i);
}
?>
<html>

<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>
<!--on this enctype attribute is important for uploading files and also mandatory-->
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td> Name
<input type="text" name="text"> </td>
</tr>
<tr>
<td> Username
<input type="text" name="user"> </td>
</tr>
<tr>
<td> password
<input type="password" name="pass"> </td>
</tr>
<tr>
<td> city
<select name="city">
<option value="">-select-</option>
<option value="knp">kanpur</option>
<option value="lko">lucknow</option>
</td>
</tr>
<tr>
<td> Gender
<input type="radio" name="gen" id="gen" value="male">male
<input type="radio" name="gen" id="gen"
value="female">female </td>
</tr>
<tr>
<td> Image
<input type="file" name="f1"> </td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="sub"> </td>
</tr>
</table>
</body>

</html>
Login.php (PHP User Login File)
Next is the login operation with MySQLi SELECT query, If you want to know more
about mysqli_num_rows which we use below you can click on the link.

<?php
include 'connect.php';
if (isset($_POST['sub']))
{
$u = $_POST['user'];
$p = $_POST['pass'];
$s = "select * from reg where username='$u' and password= '$p'";
$qu = mysqli_query($con, $s);

//Here we check if our query is correct then databse have how many rows on o/p.
if (mysqli_num_rows($qu) > 0)
{
$f = mysqli_fetch_assoc($qu);

//Here we store the value ID on session for login and fetch data of same user who is logged in.
$_SESSION['id'] = $f['id'];

//if all conditions are correct then redirects to this page.


header('location:home.php');
}
else
{
echo 'username or password does not exist';
}

}
?>
<html>

<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td> Username
<input type="text" name="user"> </td>
</tr>
<tr>
<td> password
<input type="password" name="pass"> </td>
</tr>
<tr>
<td>
<input type="submit" name="sub" value="submit"> </td>
</tr>
</table>
</body>
</html>
View user’s data
View.php

<?php
include 'connect.php';
?>
<table border='1'>
<tr>
<th> Name </th>
<th> Username </th>
</tr>

<?php
$sq="select * from reg";
$qu=mysqli_query($con,$sq);

//we use while loop for all data of the user and fetch_assoc function useful for getting data from
database
while($f= mysqli_fetch_assoc($qu)){
?>
<tr>
<td><?php echo $f['name']?></td>
<td><?php echo $f['username']?></td>
</tr>
<?php
}
?>
Home.php
On this page you can see particular user data which we get by using PHP SESSION.
<?php
include'connect.php';
//here we check the id with session id which we get on the time of login
$s="select*from reg where id='$_SESSION[id]'";
$qu= mysqli_query($con, $s);
$f=mysqli_fetch_assoc($qu);
?>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>
Name
</td>
<td>
<?php echo $f['name'];?>
</td>
</tr>
<tr>
<td> Username
</td>
<td>
<?php
echo $f['username'];?>
</td>
</tr>
<tr>
<td> Password
</td>
<td>
<?php
echo $f['password']."<br>";?>
</td>
</tr>
<tr>
<td> City
</td>
<td>
<?php
echo $f['city']."<br>";?>
</td>
</tr>
<tr>
<td>Gender
</td>
<td>
<?php
echo $f['gender']."<br>";?>
</td>
</tr>
<tr>
<td> Image
</td>
<td>
<img src="<?php
echo $f['image'];?>" width="100px" height="100px">
</td>
</tr>
</table>
<a href="edit.php">Edit
</a>
<a href="delete.php">Delete
</a>
</body>
</html>
Edit.php
See how we edit the particular user data using UPDATE query and SESSION.
<?php
include'connect.php';
if(isset($_POST['sub'])){
$t=$_POST['text'];
$u=$_POST['user'];
$p=$_POST['pass'];
$c=$_POST['city'];
$g=$_POST['gen'];
if($_FILES['f1']['name']){
move_uploaded_file($_FILES['f1']['tmp_name'], "image/".$_FILES['f1']['name']);
$img="image/".$_FILES['f1']['name'];
}
else{
$img=$_POST['img1'];
}
$i="update reg set name='$t',username='$u',password='$p',city='$c',gender='$g',image='$img' where
id='$_SESSION[id]'";
mysqli_query($con, $i);
header('location:home.php');
}
$s="select*from reg where id='$_SESSION[id]'";
$qu= mysqli_query($con, $s);
$f=mysqli_fetch_assoc($qu);
?>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>
Name
<input type="text" name="text" value="<?php echo $f['name']?>">
</td>
</tr>
<tr>
<td>
Username
<input type="text" name="user" value="<?php echo $f['username']?>">
</td>
</tr>
<tr>
<td>
password
<input type="password" name="pass" value="<?php echo $f['password']?>">
</td>
</tr>
<tr>
<td>
city
<select name="city">
<option value="">-select-
</option>
<option value="knp"
<?php if($f['city']=='knp'){ echo "selected='selected'";}?>>kanpur</option>
<option value="lko"<?php if($f['city']=='lko'){ echo "selected='selected'";}?>>lucknow</option>
</td>
</tr>
<tr>
<td>
Gender
<?php if($f['gender']=='male'){
?>
<input type="radio"name="gen" id="gen" value="male" checked>
<?php
} else {
?>
<input type="radio"name="gen" id="gen" value="male">
<?php } ?>male
<?php if($f['gender']=='female'){
?>
<input type="radio"name="gen" id="gen" value="female" checked>
<?php
} else {
?>
<input type="radio"name="gen" id="gen" value="female">
<?php } ?>female
</td>
</tr>
<tr>
<td>
Image
<img src="<?php echo $f['image']?>" width="100px" height="100px">
<input type="file" name="f1">
<input type="hidden" name="img1" value="<?php echo $f['image']?>">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="sub">
</td>
</tr>
</table>
</form>

Last is delete file. DELETE.php

<?php

include 'connect.php';

$sq="delete from reg where id='$_SESSION[id]'";

mysqli_query($con,$sq);

header('location:add_district.php');

?>

Conclusion
Above is complete CRUD operation with image uploading and Login Registration operation
with some easy manner.

You might also like