Project Update Delete

 Project 1

koneksi.php
<?php 
$db = mysqli_connect("localhost", "root", "", "nov4");

if (!$db) {

    echo "Koneksi Gagal";

}

function hapus ($data) {

    global $db;

    $id = $_GET['id'];

    $sql = "DELETE FROM table1 WHERE id = '$id'";
    $query = mysqli_query($db, $sql);
    
    return mysqli_affected_rows($db);

}

function edit($data) {

    global $db;

    $id = $_GET['id'];
    $nama = $_POST['nama'];

    $sql = "UPDATE table1 SET
            nama = '$nama'
            WHERE id = '$id'";

    $query = mysqli_query($db, $sql);

    return mysqli_affected_rows($db);

}    
?>

form.php
<!DOCTYPE html>

<html>

<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
    <title>Update Delete</title>

</head>

<body>

    <center>

    <table border="3" cellpadding="5">

        <tr>

            <th>ID</th>

            <th>Nama</th>

            <th>Aksi</th>

        </tr>

        <?php 

        include 'koneksi.php';

            $sql = mysqli_query($db, "SELECT * FROM table1");

            foreach ($sql as $data) {

         ?>

        <tr>

            <td><?php echo $data["id"];?></td>

            <td><?php echo $data["nama"];?></td>

            <td><a href="hapus.php?id=<?=$data["id"];?>"><span class="material-symbols-outlined">delete</span></a>

                <a href="edit.php?id=<?=$data["id"];?>"><span class="material-symbols-outlined">edit</span></a></td>

        </tr>

         <?php } ?>

    </table></center>

</body>

edit.php
<?php
    include 'koneksi.php';
    $id = $_GET['id'];

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

        if (edit($_POST)>0) {
            echo "<script>alert('data berhasil diedit');
        document.location.href = 'form.php';
        </script>";

        }else{
            echo "<script>alert('data gagal diedit');
        </script>";
        }
    }
?>
 <!DOCTYPE html>

<html>

<head>

    <title></title>

</head>

<style type="text/css">

    table{

        border-radius: 10%;

       border: 2px solid;

       border-color: black;

       margin: 10px;

    }

</style>

<body>

    <center>

    <form method="POST">

        <?php 

            $sql = mysqli_query($db,"SELECT * FROM table1 WHERE id = '$id'");

            foreach ($sql as $row) {                

         ?>

         <table cellpadding="13">

             <tr>

                 <th colspan="2"><h2>EDIT DATA</h2></th>

             </tr>

             <tr>

                 <td colspan="2"><input type="number" name="id" value="<?php echo $id; ?>" hidden></td>

             </tr>

             <tr>

                 <td><label>Nama</label></td>

                 <td><input type="text" name="nama" id="nama" value="<?php echo $row["nama"];?>"></td>

             </tr>

             <tr>

                 <td colspan="2"><input type="submit" name="submit"></td>

             </tr>

         </table>

        <?php } ?>

    </form>

    </center>

</body>

</html>

hapus.php
<?php
    include 'koneksi.php';

    $id = $_GET['id'];

    if (hapus($id)>0) {
        echo "
        <script>alert('data berhasil dihapus');
        document.location.href = 'form.php';
        </script>";
    }else{
        echo "
        <script>alert('data gagal dihapus');
        </script>";
    }
?>

Project 2

koneksi.php
<?php
    $conn = mysqli_connect("localhost", "root", "", "nov4");

function hapus ($data) {
    global $conn;

    $id = $_GET['id'];
    $sql = "DELETE FROM table2 WHERE id = '$id'";
    $query = mysqli_query($conn, $sql);
    
    return mysqli_affected_rows($conn);

}

function edit($data) {

    global $conn;

    $id = $_GET['id'];
    $nama = $_POST['nama'];
    $jk = $_POST['jk'];

    $sql = "UPDATE table2 SET
            nama = '$nama',
            jk = '$jk'
            WHERE id = '$id'";

    $query = mysqli_query($conn, $sql);

    return mysqli_affected_rows($conn);

}

index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
    <title>Project 2</title>
</head>
<body>
    <h1>Daftar Siswa</h1>
    <table border="3">
        <tr>
            <th>ID</th>
            <th>Nama</th>
            <th>Jenis Kelamin</th>
            <th>Aksi</th>
        </tr>
        <?php 
            include 'koneksi.php';
            $siswa = mysqli_query($conn, "select * from table2");
            foreach ($siswa as $data) {
        ?>
        <tr>
            <td><?php echo $data['id'];?></td>
            <td><?php echo $data['nama'];?></td>
            <td><?php echo $data['jk'];?></td>
            <td>
                <a href="edit.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">edit</span></a>
                <a href="hapus.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">delete</span></a>
            </td>
        </tr>
        <?php }?>
    </table>
</body>
</html>

edit.php
<?php
include 'koneksi.php';
$id = $_GET['id'];

if (isset($_POST['submit'])) {
    if (edit($_POST) > 0) {
        echo "<script>alert('data berhasil diedit');
        document.location.href = 'index.php';
        </script>";
    } else {
        echo "<script>alert('data gagal diedit');
        </script>";
    }
}
?>
<!DOCTYPE html>

<html>

<head>
    <title></title>
</head>
<style type="text/css">
    table {
        border-radius: 10%;
        border: 2px solid;
        border-color: black;
        margin: 10px;
    }
</style>

<body>

    <center>
        <form method="POST">
            <?php
            $sql = mysqli_query($conn, "SELECT * FROM table2 WHERE id = '$id'");
            foreach ($sql as $row) {
            ?>
                <table cellpadding="13">
                    <tr>
                        <th colspan="2">
                            <h2>EDIT DATA</h2>
                        </th>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="number" name="id" value="<?php echo $id; ?>" hidden></td>
                    </tr>
                    <tr>
                        <td>
                            <label>Nama :</label>
                            <input type="text" name="nama" id="nama" value="<?php echo $row["nama"]; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Jenis kelamin :</label>
                            <?php 
                            $jk = $row['jk'];  
                                if ($jk== "Laki-laki") {          
                            
                            echo '<input type="radio" name="jk" id="laki" value="laki-laki" checked>Laki-laki
                                <input type="radio" name="jk" id="perempuan" value="perempuan">Perempuan';
                             
                                } elseif ($jk=="Perempuan") {
                        
                            echo '<input type="radio" name="jk" id="laki" value="Perempuan"checked>Perempuan
                            <input type="radio" name="jk" id="perempuan" value="Laki-laki">Laki-laki';
                                    
                        } ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submit"></td>
                    </tr>
                </table>
            <?php } ?>
        </form>
    </center>
</body>

</html>

hapus.php
<?php
    include 'koneksi.php';

    $id = $_GET['id'];

    if (hapus($id)>0) {
        echo "
        <script>alert('data berhasil dihapus');
        document.location.href = 'index.php';
        </script>";
    }else{
        echo "
        <script>alert('data gagal dihapus');
        </script>";
    }
?>

Project 3

koneksi.php
<?php 
$conn = mysqli_connect("localhost", "root", "", "nov4");

function hapus($data){
    global $conn;
    $id = $_GET['id'];

    $sql = mysqli_query($conn, "delete from table4 where id='$id'");
    return mysqli_affected_rows($conn);
}

function edit($data){
    global $conn;
    $id= $_GET['id'];
    $nama = $_POST['nama'];
    $hobi = $_POST['hobi'];
    $result = implode(",",$hobi);

    $sql = "update table4 set nama= '$nama', hobi= '".$result."' where id='$id'";
    $query = mysqli_query($conn, $sql);
    return mysqli_affected_rows($conn); 
}
?>

index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
    <title>Project 2</title>
</head>
<body>
    <h1>Daftar Siswa</h1>
    <table border="3">
        <tr>
            <th>ID</th>
            <th>Nama</th>
            <th>Jenis Kelamin</th>
            <th>Aksi</th>
        </tr>
        <?php 
            include 'koneksi.php';
            $siswa = mysqli_query($conn, "select * from table3");
            foreach ($siswa as $data) {
        ?>
        <tr>
            <td><?php echo $data['id'];?></td>
            <td><?php echo $data['nama'];?></td>
            <td><?php echo $data['kelas'];?></td>
            <td>
                <a href="edit.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">edit</span></a>
                <a href="hapus.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">delete</span></a>
            </td>
        </tr>
        <?php }?>
    </table>
</body>
</html>

edit.php
<?php
include 'koneksi.php';
$id = $_GET['id'];

if (isset($_POST['submit'])) {
    if (edit($_POST) > 0) {
        echo "<script>alert('data berhasil diedit');
        document.location.href = 'index.php';
        </script>";
    } else {
        echo "<script>alert('data gagal diedit');
        </script>";
    }
}
?>
<!DOCTYPE html>

<html>

<head>
    <title></title>
</head>
<style type="text/css">
    table {
        border-radius: 10%;
        border: 2px solid;
        border-color: black;
        margin: 10px;
    }
</style>

<body>

    <center>
        <form method="POST">
            <?php
            $sql = mysqli_query($conn, "SELECT * FROM table3 WHERE id = '$id'");
            foreach ($sql as $row) {
            ?>
                <table cellpadding="13">
                    <tr>
                        <th colspan="2">
                            <h2>EDIT DATA</h2>
                        </th>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="number" name="id" value="<?php echo $id; ?>" hidden></td>
                    </tr>
                    <tr>
                        <td>
                            <label>Nama :</label>
                            <input type="text" name="nama" id="nama" value="<?php echo $row["nama"]; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <select name="kelas" id="kelas">
                                <option value="<?php echo $row['kelas']; ?>" selected><?php echo $row['kelas']; ?></option>
                                <option value="X">X</option>
                                <option value="XI">XI</option>
                                <option value="XII">XII</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submit"></td>
                    </tr>
                </table>
            <?php } ?>
        </form>
    </center>
</body>

</html>

hapus.php
<?php
    include 'koneksi.php';

    $id = $_GET['id'];

    if (hapus($id)>0) {
        echo "
        <script>alert('data berhasil dihapus');
        document.location.href = 'index.php';
        </script>";
    }else{
        echo "
        <script>alert('data gagal dihapus');
        </script>";
    }
?>

Project 4

koneksi.php
<?php 
$conn = mysqli_connect("localhost", "root", "", "nov4");

function hapus($data){
    global $conn;
    $id = $_GET['id'];

    $sql = mysqli_query($conn, "delete from table4 where id='$id'");
    return mysqli_affected_rows($conn);
}

function edit($data){
    global $conn;
    $id= $_GET['id'];
    $nama = $_POST['nama'];
    $hobi = $_POST['hobi'];
    $result = implode(",",$hobi);

    $sql = "update table4 set nama= '$nama', hobi= '".$result."' where id='$id'";
    $query = mysqli_query($conn, $sql);
    return mysqli_affected_rows($conn); 
}
?>

index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
    <title>Document</title>
</head>
<body>
    <table border="3">
        <tr>
            <th>ID</th>
            <th>Nama</th>
            <th>Hobi</th>
            <th>Aksi</th>
        </tr>
        <?php 
            include 'koneksi.php';
            $sql = mysqli_query($conn, "select * from table4");
            foreach ($sql as $data) {
        ?>
        <tr>
            <td><?php echo $data['id']; ?></td>
            <td><?php echo $data['nama']; ?></td>
            <td>
                <?php echo $data['hobi']; ?>
            </td>
            <td>
                <a href="hapus.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">delete</span></a>
                <a href="edit.php?id=<?=$data['id']?>"><span class="material-symbols-outlined">edit</span></a>
            </td>
        </tr>
        <?php } ?>
    </table>
</body>
</html>

edit.php
<?php
    include 'koneksi.php';
    $id = $_GET['id'];

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

        if (edit($_POST)>0) {
            echo "<script>alert('data berhasil diedit');
        document.location.href = 'index.php';
        </script>";

        }else{
            echo "<script>alert('data gagal diedit');
        </script>";
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>Edit Data</h2>
    <form action="" method="post">
        <table>
            <?php 
                $sql = mysqli_query($conn, "select * from table4 where id='$id'");
                foreach ($sql as $row) {
            ?>
            <tr>
                <td>Nama</td>
                <td><input type="text" name="nama" value="<?php echo $row['nama']; ?>"></td>
            </tr>
            <tr>
                <td>Hobi</td>
                <?php $hobi=$row['hobi'];
                $space = explode(",",$hobi);
                ?>
                <td>
                    <input type="checkbox" name="hobi[]" value="Main Bola" <?php
                        if (in_array("Main Bola", $space)) {
                            echo "checked";
                        }
                    ?>>
                    <label>Main Bola</label>
                    <input type="checkbox" name="hobi[]" value="Berenang" <?php
                        if (in_array("Berenang", $space)) {
                            echo "checked";
                        }
                    ?>>
                    <label>Berenang</label>
                    <input type="checkbox" name="hobi[]" value="Main Game" <?php
                        if (in_array("Main Game", $space)) {
                            echo "checked";
                        }
                    ?>>
                    <label>Main Game</label>
                    <input type="checkbox" name="hobi[]" value="Slebew" <?php
                        if (in_array("Slebew", $space)) {
                            echo "checked";
                        }
                    ?>>
                    <label>Slebew</label>
                </td>
            </tr>
            <?php }?>
            <tr>
                <td>
                    <button type="submit" name="submit">Ubah Data</button>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

hapus.php
<?php
    include 'koneksi.php';

    $id = $_GET['id'];

    if (hapus($id)>0) {
        echo "
        <script>alert('data berhasil dihapus');
        document.location.href = 'index.php';
        </script>";
    }else{
        echo "
        <script>alert('data gagal dihapus');
        </script>";
    }
?>

Comments

Popular posts from this blog

APLIKASI PERPUSTAKAAN DENGAN BOOTSTRAP 5, PHP DAN MYSQL

Aplikasi PKL dengan PHP