PHP CRUD Operation Using MySQLi (Source Code)

Read it completely from here: https://phpcoder.tech/php-crud-operation-using-mysqli-source-code/

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.

<?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

Read it completely from here: https://phpcoder.tech/php-crud-operation-using-mysqli-source-code/

Also Read:

Happy Coding…!

I am a web application developer. FB Community: @mrpandatech, Twitter: @phpcodertech My PHP problem-solving site http://phpcoder.tech