ads_header

Tutorial CRUD With PHP & MySQL

Tutorial CRUD With PHP & MySQL


Hello guys, in this post I will discuss how to create a simple CRUD program using PHP and MySQL. CRUD is a program that is generally always present in large or small applications. Because this program is responsible for managing user data that is entered so it is very important to learn if it is just starting to create a basic program.





The following tutorial is very easy to understand, you need it automatically. There are many tutorials that discuss this, but not many screenshoots are included at each stage. And before I have created a database with the name "example" and a table with the name "student".


Now, create a new folder in the www folder (if you use Appserv) or htdocs (if you use xamp). Then in that folder create a .php file and name it "config.php" to make a connection to the database.

<?php 

$connect = mysqli_connect("localhost","root","root","example");

// Check connection
if (mysqli_connect_errno()){
    echo "connection failure : " . mysqli_connect_error();
}

?>

Next, create the main page and name it "index.php" and enter the following code

<!DOCTYPE html>
<html>

<head>
    <title>CRUD PHP dan MySQLi</title>
</head>

<body>

    <h2>CRUD STUDENT</h2>
    <br/>
    <a href="add.php">+ ADD STUDENT</a>
    <br/>
    <br/>
    <table border="1">
        <tr>
            <th style="padding:10px">ID</th>
            <th style="padding:10px">Name</th>
            <th style="padding:10px">Major</th>
            <th style="padding:10px">Address</th>
            <th style="padding:10px">Action</th>
        </tr>
        <?php 
  include 'config.php';
  $no = 1;
  $data = mysqli_query($connect,"select * from student");
  while($d = mysqli_fetch_array($data)){
   ?>
            <tr>
                <td style="padding:10px">
                    <?php echo $no++; ?>
                </td>
                <td style="padding:10px">
                    <?php echo $d['name']; ?>
                </td>
                <td style="padding:10px">
                    <?php echo $d['major']; ?>
                </td>
                <td style="padding:10px">
                    <?php echo $d['address']; ?>
                </td>
                <td style="padding:10px">
                    <a href="edit.php?id=<?php echo $d['id']; ?>">EDIT</a>
                    <a href="delete.php?id=<?php echo $d['id']; ?>">DELETE</a>
                </td>
            </tr>
            <?php 
  }
  ?>
    </table>
</body>

</html>

The result is more or less like the picture below

index.php

Perhatikan kode <a href="add.php">+ ADD STUDENT</a>. The code is to switch the page to "add.php" when we press the "ADD STUDENT" button

Now create a new file and name it "add.php" and enter the following code to create a form view

<!DOCTYPE html>
<html>

<head>
    <title>CRUD PHP dan MySQLi</title>
</head>

<body>

    <h2>CRUD STUDENT</h2>
    <br/>
    <a href="index.php">BACK</a>
    <br/>
    <br/>
    <h3>ADD STUDENT</h3>
    <form method="post" action="add_action.php">
        <table>
            <tr>
                <td>Name</td>
                <td>
                    <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td>Major</td>
                <td>
                    <input type="text" name="major">
                </td>
            </tr>
            <tr>
                <td>Address</td>
                <td>
                    <input type="text" name="address">
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="SAVE">
                </td>
            </tr>
        </table>
      </form>
</body>

</html>

add.php

Pay attention to the code <form method="post" action="add_action.php">. The code will process the file "add_action.php" which functions to save data to the database inputted in the form "add.php" when we press the "SAVE" button





Now, create the file "add_action.php" and enter the following code

<?php 
// connect database
include 'config.php';

// retrieve data from the form
$name = $_POST['name'];
$major = $_POST['major'];
$address = $_POST['address'];

// input data into the database
mysqli_query($connect,"insert into student values('','$name','$major','$address')");

// return to the index.php page
header("location:index.php");

?>

Until here, we have successfully entered the data in the database and then see it on the main page

index.php
Now, pay attention to the code
<a href="edit.php?id=<?php echo $d['id']; ?>">EDIT</a> in "index.php".
The code is to switch to the "edit.php" page and display data based on ID.

Now, create the file "edit.php" and enter the following code

<!DOCTYPE html>
<html>

<head>
    <title>CRUD PHP dan MySQLi</title>
</head>

<body>

    <h2>CRUD STUDENT</h2>
    <br/>
    <a href="index.php">BACK</a>
    <br/>
    <br/>
    <h3>EDIT STUDENT</h3>

    <?php
 include 'config.php';
 $id = $_GET['id'];
 $data = mysqli_query($connect,"select * from student where id='$id'");
 while($d = mysqli_fetch_array($data)){
  ?>
        <form method="post" action="update.php">
            <table>
                <tr>
                    <td>Name</td>
                    <td>
                        <input type="hidden" name="id" value="<?php echo $d['id']; ?>">
                        <input type="text" name="name" value="<?php echo $d['name']; ?>">
                    </td>
                </tr>
                <tr>
                    <td>Major</td>
                    <td>
                        <input type="text" name="major" value="<?php echo $d['major']; ?>">
                    </td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td>
                        <input type="text" name="address" value="<?php echo $d['address']; ?>">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="SAVE">
                    </td>
                </tr>
            </table>
        </form>
        <?php 
 }
 ?>

</body>

</html>

edit.php
Now, pay attention to the code <form method="post" action="update.php">.
The code functions for the process of storing data that we change in the form "edit.php" when we press the "SAVE" button

Now, create a new file with the name "update.php" then enter the following code

<?php 
// connect database
include 'config.php';

$id = $_POST['id'];
$name = $_POST['name'];
$major = $_POST['major'];
$address = $_POST['address'];

// update data
mysqli_query($connect,"update student set name='$name', major='$major', address='$address' where id='$id'");

// back to index.php page
header("location:index.php");

?>

Up here, we have succeeded in making changes to the data

data after being changed
Now, pay attention to the code
<a href="delete.php?id=<?php echo $d['id']; ?>">DELETE</a> in "index.php".

The code is to delete data based on ID.

Now, create the file "delete.php" and enter the following code


<?php 
// connect database
include 'config.php';


$id = $_GET['id'];


// remove data from database
mysqli_query($connect,"delete from student where id='$id'");

// back to index.php page
header("location:index.php");

?>

Now, the data is successfully deleted

successfully deleted data

Powered by Blogger.