Bagaimana Cara menghapus banyak data dengan select box ??

Halo semuanya,...

Ada yang tau gimana caranya menghapus banyak data dengan bantuan select box

Ilustrasi gambarnya seperti ini

Menggunakan : Bahasa program PHP 5 (Native) Metode Database MYSQLi (Prosedural)

Terimakasih sudah membantu #luaskanilmuluaskanmanfaat

avatar AjieBaskoro
@AjieBaskoro

24 Kontribusi 4 Poin

Diperbarui 6 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Coba code ini gan:

 <?php
    $host = 'localhost'; // Host name
    $username = ''; // Mysql username
    $password = ''; // Mysql password
    $db_name = 'test'; // Database name
    $tbl_name = 'test_mysql'; // Table name

    // Connect to server and select databse.
    mysql_connect($host, $username, $password) or die('cannot connect');
    mysql_select_db($db_name) or die('cannot select DB');

    $sql = 'SELECT * FROM `'.$tbl_name.'`';
    $result = mysql_query($sql);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <form name="form1" method="post" action="">
            <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
            <tr>
                <td bgcolor="#FFFFFF"> </td>
                <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
            </tr>
            <tr>
                <td align="center" bgcolor="#FFFFFF">#</td>
                <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
            </tr>
            <?php while ($rows = mysql_fetch_array($result)): ?>
            <tr>
                <td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
                <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
                <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['name']); ?></td>
                <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['lastname']); ?></td>
                <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['email']); ?></td>
            </tr>
            <?php endwhile; ?>
            <tr>
                <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
            </tr>
            <?php
                // Check if delete button active, start this
                if ( ! empty($_POST['delete'])) {
                    foreach ($_POST['need_delete'] as $id => $value) {
                        $sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;
                        mysql_query($sql);
                    }
                    header('Location: delete_multiple.php'); exit();
                }
                mysql_close();
            ?>
            </table>
            </form>
        </td>
    </tr>
</table>

Source: [link]https://stackoverflow.com/questions/6388613/delete-multiple-rows-with-checkbox-form-db-table [/link]

Atau Coba beberapa tutorial ini: 1. how to delete multiple records in php using checkbox Link: https://www.youtube.com/watch?v=RFZXcS6T0go 2. Delete Multiple Rows using Checkbox in PHP and MySQL Link: https://www.youtube.com/watch?v=j56UebhZkcI

avatar keccikun
@keccikun

368 Kontribusi 197 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban