Pengembangan CRUD1 - Hallo sahabat Dev-Create, Pada Artikel yang anda baca kali ini dengan judul Pengembangan CRUD1, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel Materi CRUD, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Pengembangan CRUD1
link : Pengembangan CRUD1

Baca juga


Pengembangan CRUD1

Berikut adalah hasil pengembangan CRUD1

Sebelu masuk materi pastikan database sudah di buat :


1. Membuat file Config atau file koneksi

file : config.php

File asli

<?php
/**
 * using mysqli_connect for database connection
 */

$databaseHost = 'localhost';
$databaseName = 'crud_db';
$databaseUsername = 'root';
$databasePassword = '';

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

?>

Ket : File diatas adalah untuk mengoneksi ke database MySQL, apabila terkoneksi maka ditampilan di web browser tidak ada tampila apa-apa berikut juga jika error.

File Pengembangan

<?php
/**
 * using mysqli_connect for database connection
 */

$databaseHost = 'localhost';
$databaseName = 'crud_db';
$databaseUsername = 'root';
$databasePassword = '';

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


if (mysqli_connect_errno()){
 echo "Koneksi database gagal : " . mysqli_connect_error();
}
# file pengembagan

?>


Ket : ditambahkan keterangan jika terjadi error satau tidak konek maka ada notifikasinya.


Pengembangan
file : config.php


<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'crud_db');

if (mysqli_connect_errno()) {
  echo "Koneksi gagal : ".mysqli_connect_error();
}
?>

Ket : Variabel untuk koneksi lebih ringkas



2. Membuat Halaman Index


File : Asli

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

Ket : Tidak ada kolom nomor yang urut
=============================================================

Pengembanagan :

<?php
// Create database connection using config file
include_once("koneksi2.php");

// Fetch all users data from database
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>

<html>
<head> 
    <title>Homepage</title>
</head>

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

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

    <tr>
        <th>Nomor</th> <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
    </tr>
    <?php
    $nomor =1;
    while($user_data = mysqli_fetch_array($result)) {       
        echo "<tr>";
        echo "<td>".$nomor ++."</td>";
        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>

Ket : Ada kolom nomor yang urut pada tabel

==================================================================

3. Halaman Input Data


file : 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="Tambah"></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>

===============================================================

Pengembangan : Dibawah ini didapat logika menamilkan ke layar ,
sehingga di ketahaui alur jika di kirim ke layar atau ke database :

<html>
<head>
    <title>Add Users</title>
</head>

<body>


    <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="Tambah"></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'];

        echo "$name";
        echo "<br>";
        echo "$email";
        echo "<br>";
        echo "$mobile";

    }
    ?>

</body>
</html>


4. Membuat Halaman EDIT



File Asli:
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>

Keterangan : pada kode ini data yang ditampilkan mau di edit data setelah spasi hilang.
penyebabnya tidak di kasih tada ("") pada input text.

===============================================================

Jika di ubah seperti di bawah ini data setelah spasi akan muncul,
berikut perubahaya : 


<?php
// include database connection file
include_once("config.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>

<?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");
}
?>


5. Membuat Halaman Delete

file : 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");
?>


6. Membuat Halaman CARI DATA

Dari hasil pengembangan di peroleh cara tapilkan data, ada beberapa tahapan agar file tersebut sempurna. Berikut hasilnya :

a. Pilihan ke Satu
   file : cari.php

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>


<?php
require_once'config.php';
?>

<form method="post">
<input type="text" name="nt" placeholder="Masukan Nama">
<input type="submit" name="submit" value="cari">
</form>
<br/>
<br/>

<table border=1>
<tr> <td>NAMA</td><td>Email</td><td>No HP</td></tr>

<?php
if(!ISSET($_POST['submit'])){

$sql = "SELECT * FROM users";

$query = mysqli_query($mysqli, $sql);
while ($row = mysqli_fetch_array($query)){
?>


<tr>
 <td><?php echo $row['name']; ?></td>
 <td><?php echo $row['email']; ?></td>
 <td><?php echo $row['mobile']; ?></td>
</tr>


<?php } } ?>

<?php if (ISSET($_POST['submit'])){
 $cari = $_POST['nt'];
 $query2 = "SELECT * FROM users WHERE name LIKE '%$cari%'";

 $sql = mysqli_query($mysqli, $query2);
 while ($r = mysqli_fetch_array($sql)){
  ?>

 <tr>
 <td><?php echo $r['name']; ?></td>
 <td><?php echo $r['email']; ?></td>
 <td><?php echo $r['mobile']; ?></td>
</tr>
 <?php }} ?>

<table>
<?php






?>
</table>

</body>
</html>

============================================================

b. Pilihan Ke Dua
 
   Perbedaanya data tidak di tampilkan di bawahya, baru setelah di cari hasilnya muncul.

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>


<?php
require_once'config.php';
?>

<form method="post">
<input type="text" name="nt" placeholder="Masukan Nama">
<input type="submit" name="submit" value="cari">
</form>
<br/>
<br/>


<?php

 if (ISSET($_POST['submit'])){
 $cari = $_POST['nt'];
 $query2 = "SELECT * FROM users WHERE name LIKE '%$cari%'";

 $sql = mysqli_query($mysqli, $query2);
 while ($r = mysqli_fetch_array($sql)){
  ?>

 <table border=1>
 <tr> <td>NAMA</td><td>Email</td><td>No HP</td></tr>
 <tr>
 <td><?php echo $r['name']; ?></td>
 <td><?php echo $r['email']; ?></td>
 <td><?php echo $r['mobile']; ?></td>
</tr>
</table>

 <?php }} ?>




</body>
</html>

c. Pilihan ke tiga


apabila setelah di cari data muncul tanpa tabel

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>


<?php
require_once'config.php';
?>

<form method="post">
<input type="text" name="nt" placeholder="Masukan Nama">
<input type="submit" name="submit" value="cari">
</form>
<br/>
<br/>


<?php

 if (ISSET($_POST['submit'])){
 $cari = $_POST['nt'];
 $query2 = "SELECT * FROM users WHERE name LIKE '%$cari%'";

 $nomor=1;
 $sql = mysqli_query($mysqli, $query2);
 while ($r = mysqli_fetch_array($sql)){

 echo "Nomor : " .$nomor++."<br>";
 echo "Nama Pelanggan : ".$r['name']."<br>";
 echo "Alamat Email : ".$r['email']."<br>";
 echo "No TLP : ".$r['mobile']."<br><br>";


  }} ?>


</body>
</html>

d. Pilihan ke empat


Data muncul dengan tabel dan ada opsi update delete

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>


<?php
require_once'config.php';
?>

<form method="post">
<input type="text" name="nt" placeholder="Masukan Nama">
<input type="submit" name="submit" value="cari">
</form>
<br/>
<br/>


<?php

 if (ISSET($_POST['submit'])){
 $cari = $_POST['nt'];
 $query2 = "SELECT * FROM users WHERE name LIKE '%$cari%'";


 $nomor=1;
 $sql = mysqli_query($mysqli, $query2);
 while ($r = mysqli_fetch_array($sql)){
  ?>

<?php $t=$r['id']; ?>

 <table border=1>
 <tr> <td>NOMOR</td><td>NAMA</td><td>EMAIL</td><td>NO TLP</td><td>OPSI</td></tr>
 <tr>
 <td><?php echo $nomor++; ?></td>
 <td><?php echo $r['name']; ?></td>
 <td><?php echo $r['email']; ?></td>
 <td><?php echo $r['mobile']; ?></td>
 <td><?php echo "<a href='edit.php?id=$r[id]'>Edit</a> | <a href='delete.php?id=$r[id]'>Delete</a>"."<br><br>";?>  </td>

</tr>
</table>

<?php echo "<br>"?>


 <?php }} ?>




</body>
</html>


e. Pilihan ke lima

Data muncul tanpa tabel dengan opsi update delete

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>


<?php
require_once'config.php';
?>

<form method="post">
<input type="text" name="nt" placeholder="Masukan Nama">
<input type="submit" name="submit" value="cari">
</form>
<br/>
<br/>


<?php

 if (ISSET($_POST['submit'])){
 $cari = $_POST['nt'];
 $query2 = "SELECT * FROM users WHERE name LIKE '%$cari%'";

 $nomor=1;
 $sql = mysqli_query($mysqli, $query2);
 while ($r = mysqli_fetch_array($sql)){


 echo "Nomor : " .$nomor++."<br>";
 echo "Nama Pelanggan : ".$r['name']."<br>";
 echo "Alamat Email : ".$r['email']."<br>";
 echo "No TLP : ".$r['mobile']."<br>";
 echo "<a href='edit.php?id=$r[id]'>Edit</a> | <a href='delete.php?id=$r[id]'>Delete</a>"."<br><br>";

  }} ?>


</body>
</html>







Demikianlah Artikel Pengembangan CRUD1

Sekianlah artikel Pengembangan CRUD1 kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Pengembangan CRUD1 dengan alamat link https://dev-create.blogspot.com/2020/02/pengembangan-crud1.html