Cara Membuat CRUD dengan Menggunakan php dan mysql

Cara Membuat CRUD dengan Menggunakan php dan mysql


Aktifkan dulu Apache dan Mysql nya di Xampp dan Buat Folder dengan nama crud di -> C:/Xampp/Htdocs/crud

1. Buka di tab browser dan ketikan alamat -> localhost/phpmyadmin

   - Cari Tab SQL dan Buatlah database dengan nama database -> crud_db

   Perintah untuk membuat database :

   -------------------------

   create database crud_db;

   -------------------------

2. Didalam database crud_db,  Cari Tab SQL dan buatlah tabel dengan    nama -> users

   Perintah untuk membuat tabel :

   -------------------------

   CREATE TABLE IF NOT EXISTS `users` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(100) DEFAULT NULL,

  `email` varchar(100) DEFAULT NULL,

  `mobile` varchar(15) DEFAULT NULL,

  PRIMARY KEY (`id`)

   ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;# MySQL    returned an empty result set (i.e. zero rows).



   --

   -- Dumping data for table `users`

   --


    INSERT INTO `users` (`id`, `name`, `email`, `mobile`) VALUES

    (1, 'sukma', 'ipal123456@gmail.com', '08901');# 1 row(s) affected.


   -------------------------

3. Buatlah file koneksi di notepad/sublime text dengan nama-> config.php

   -------------------------

   <?php



   $databaseHost = 'localhost';

   $databaseName = 'crud_db';

   $databaseUsername = 'root';

   $databasePassword = '';


   $mysqli = mysqli_connect($databaseHost, $databaseUsername,    $databasePassword, $databaseName); 


    ?>

   ---------------------------

4. Buatlah Halaman Utama dengan nama index.php

   ---------------------------

   <?php

// Create database connection using config file

include_once("config.php");


// Fetch all users data from database

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");

?>


<html>

<head>    

    <title>Halaman Utama</title>

</head>


<body>

<a href="add.php">Add New User</a><br/><br/>


    <table width='80%' border=1>


    <tr>

        <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>

    </tr>

    <?php  

    while($user_data = mysqli_fetch_array($result)) {         

        echo "<tr>";

        echo "<td>".$user_data['name']."</td>";

        echo "<td>".$user_data['mobile']."</td>";

        echo "<td>".$user_data['email']."</td>";    

        echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a href='delete.php?id=$user_data[id]'>Delete</a></td></tr>";        

    }

    ?>

    </table>

</body>

</html>

----------------------------

5. Buatlah file Input data di notepad/sublime text dan simpan dengan nama-> add.php

----------------------------

<html>

<head>

    <title>Add Users</title>

</head>


<body>

    <a href="index.php">Go to Home</a>

    <br/><br/>


    <form action="add.php" method="post" name="form1">

        <table width="25%" border="0">

            <tr> 

                <td>Name</td>

                <td><input type="text" name="name"></td>

            </tr>

            <tr> 

                <td>Email</td>

                <td><input type="text" name="email"></td>

            </tr>

            <tr> 

                <td>Mobile</td>

                <td><input type="text" name="mobile"></td>

            </tr>

            <tr> 

                <td></td>

                <td><input type="submit" name="Submit" value="Add"></td>

            </tr>

        </table>

    </form>


    <?php


    // Check If form submitted, insert form data into users table.

    if(isset($_POST['Submit'])) {

        $name = $_POST['name'];

        $email = $_POST['email'];

        $mobile = $_POST['mobile'];


        // include database connection file

        include_once("config.php");


        // Insert user data into table

        $result = mysqli_query($mysqli, "INSERT INTO users(name,email,mobile) VALUES('$name','$email','$mobile')");


        // Show message when user added

        echo "User added successfully. <a href='index.php'>View Users</a>";

    }

    ?>

</body>

</html>

-----------------------------

6. Buatlah file untuk mengedit data di notepad/sublime text dengan nama ->edit.php

-----------------------------

<?php

// include database connection file

include_once("config.php");


// Check if form is submitted for user update, then redirect to homepage after update

if(isset($_POST['update']))

{   

    $id = $_POST['id'];


    $name=$_POST['name'];

    $mobile=$_POST['mobile'];

    $email=$_POST['email'];


    // update user data

    $result = mysqli_query($mysqli, "UPDATE users SET name='$name',email='$email',mobile='$mobile' WHERE id=$id");


    // Redirect to homepage to display updated user in list

    header("Location: index.php");

}

?>

<?php

// Display selected user data based on id

// Getting id from url

$id = $_GET['id'];


// Fetech user data based on id

$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");


while($user_data = mysqli_fetch_array($result))

{

    $name = $user_data['name'];

    $email = $user_data['email'];

    $mobile = $user_data['mobile'];

}

?>

<html>

<head>  

    <title>Edit User Data</title>

</head>


<body>

    <a href="index.php">Home</a>

    <br/><br/>


    <form name="update_user" method="post" action="edit.php">

        <table border="0">

            <tr> 

                <td>Name</td>

                <td><input type="text" name="name" value=<?php echo $name;?>></td>

            </tr>

            <tr> 

                <td>Email</td>

                <td><input type="text" name="email" value=<?php echo $email;?>></td>

            </tr>

            <tr> 

                <td>Mobile</td>

                <td><input type="text" name="mobile" value=<?php echo $mobile;?>></td>

            </tr>

            <tr>

                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>

                <td><input type="submit" name="update" value="Update"></td>

            </tr>

        </table>

    </form>

</body>

</html>

----------------------------

7. Buatlah file untuk menghapus data di notepad/sublime text dengan nama -> delete.php

------------------------------

<?php

// include database connection file

include_once("config.php");


// Get id from URL to delete that user

$id = $_GET['id'];


// Delete user row from table based on given id

$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");


// After delete redirect to Home, so that latest user list will be displayed.

header("Location:index.php");

?>

--------------------

8. Selesai, Silahkan lihat tampilannya di web browser dengan cara mengetikkan alamat -> localhost/crud


Comments

Popular posts from this blog

Aplikasi Laundry Dengan Visual Basic 6.0

Contoh script php lengkap

sambungan : Tutorial CSS step 2