Crud in php mysqli 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,

Crud in php mysqli code
PHP CRUD Operation Using MySQLi With Source Code

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 parameteris HOSTNAME, second is Database User Name, third is for Password and the lastparameter 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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

<?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 thisenctype attribute isimportant foruploading files andalso 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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?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><?phpecho$f['name']?></td>

  <td><?phpecho $f['username']?></td>

</tr>

<?php

}

?>

Home.php

On this page you can see particular user data which we get by using PHP SESSION.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

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

    <ahref="edit.php">Edit

    </a>

    <ahref="delete.php">Delete

    </a>

  </body>

</html>

Edit.php

See how we edit the particular user data using UPDATE query and SESSION.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

<?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="<?phpecho$f['name']?>">

      </td>

    </tr>

    <tr>

      <td>

        Username

        <input type="text"name="user"value="<?phpecho$f['username']?>">

      </td>

    </tr>

    <tr>

      <td>

        password

        <input type="password"name="pass"value="<?phpecho$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"<?phpif($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

    <?phpif($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="<?phpecho$f['image']?>" width="100px"height="100px">

    <input type="file"name="f1">

    <input type="hidden" name="img1"value="<?phpecho$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');

?>

Above is complete CRUD operation with image uploading and Login Registration operation with some easy manner.
Any query, feel free to ping me on mail Contact us or on comment.

Also Read:

  • How to Check Process Running in Windows
  • HTML: How to Comment With Example
  • JavaScript While Loop with an Example
  • Set PHP Error Reporting Into The PHP File

Happy Coding…!

Was this article helpful?

YesNo

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

Post navigation

What is CRUD operations PHP?

CRUD is an acronym for Create, Read, Update, and Delete. As the name suggests, these operations manipulate data in a database that is important to any web application's basic functionality. We can create a PHP application coupled with a MySQL database to perform these operations. Create and Read.

How do I apply for CRUD?

The CREATE operation can be performed by providing a form in the CRUD app that contains form fields like text, numbers, email for input and a submit button to add the new record to database so that, When we hit the submit button for our form, then a POST HTTP request would be sent to our API and add this new Recipe ...

What is CRUD code?

CRUD (Create, Read, Update, Delete) is an acronym for ways one can operate on stored data. It is a mnemonic for the four basic functions of persistent storage.

What are the 4 CRUD components?

CRUD Meaning: CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.