PHP Programming 9 - Database PHP MySQL CRUD UPDATE
Assaamu'alaikum
PHP MySQL Update
File latihan16.5.php
<?php include "db.php"; $sql = "SELECT * FROM data_tamu"; $result = mysqli_query($conn, $sql); if (isset($_GET['op'])=="delete"){ $id = $_GET['id']; $delete = "DELETE FROM data_tamu WHERE id=$id"; $con=mysqli_query($conn, $delete); header('location:./latihan16.4.php'); } if (mysqli_num_rows($result)>0){ $no=1; echo "<table border=1 cellpadding=5 cellspacing=0 >"; echo "<tr> <td> No</td> <td>Nama</td> <td>Alamat</td> <td>Email</td> <td>No HP</td> <td>Option</td> </tr>"; while ($row = mysqli_fetch_assoc($result)){ echo "<tr bgcolor = " . belang($no) . "> <td>" . $no . "</td> <td>" . $row["nama"] . "</td> <td>" . $row["alamat"] . "</td> <td>" . $row["email"] . "</td> <td>" . $row["no_hp"] . "</td> <td>" . "<a href=latihan16.5.php?op=delete&id=" . $row["id"] . ">Delete</a> | <a href=latihan16.5_edit.php?id=" . $row["id"] . ">Update</a></td></tr>"; $no++; } echo "</table>"; } else { echo"0 Result"; } mysqli_close ($conn); echo "<br><center>© Mahasiswa Mercu Buana Bekasi Pemrograman Web Genap 2019 </center>" ?> |
File latihan16.5_edit.php
$id = $_GET['id'];
Note : perlu di catat disini, semua file yang berkaitan dengan konektivitas ke database ($conn) harus di hubungkan ke file db.php yang pertama kali di buat dengan menggunakan perintah include.
$read = "SELECT * FROM data_tamu WHERE id=$id"; $result = mysqli_query($conn, $read); |
Sehingga perulangan while disini akan terpotong karena struktur php dan di buka lagi untuk menutup instruksi perulangan while ini (}). Ini lah yang disebut-sebut sebagai html di dalam php dan php didalam html :v.
while ($row = mysqli_fetch_assoc($result)){ ?> <form method="POST" action="./latihan16.5_update.php"> <tr> <td> <input type="hidden" name="id" value="<?php echo $row['id'];?>"> </td> <td> <input type="text" name="nama" value=" <?php echo $row['nama'];?>"> </td> <td> <input type="text" name="alamat" value="<?php echo $row['alamat'];?>"> </td> <td> <input type="text" name="email" value="<?php echo $row['email'];?>"> </td> <td> <input type="text" name="no_hp" maxlength="12" value="<?php echo $row['no_hp'];?>"></td> <td> <input type="submit" name="Submit" value="Submit"> </tr> </table> </form> <?php } ?> |
<html> <head> <title> Mahasiswa Mercu Buana Bekasi Pemrograman Web Genap 2019 </title> </head> <body> <?php include "db.php"; $id = $_GET['id']; $read = "SELECT * FROM data_tamu WHERE id=$id"; $result = mysqli_query($conn, $read); echo "<table border=1 cellpadding=5 cellspacing=0 >"; echo "<tr> <td ></td> <td>Nama</td> <td>Alamat</td> <td>Email</td> <td>No HP</td> <td>Update</td> </tr>"; while ($row = mysqli_fetch_assoc($result)){ ?> <form method="POST" action="./latihan16.5_update.php"> <tr> <td> <input type="hidden" name="id" value="<?php echo $row['id'];?>"> </td> <td> < input type="text" name="nama" value="<?php echo $row['nama'];?>"> </td> <td> <input type="text" name="alamat" value="<?php echo $row['alamat'];?>"> </td> <td> <input type="text" name="email" value="<?php echo $row['email'];? >"> </td> <td> <input type="text" name="no_hp" maxlength="12" value="<?php echo $row['no_hp'];?>"></td> <td> <input type="submit" name="Submit" value="Submit"> </tr> </table> </form> <?php } ?> <p align=center> © Mahasiswa Mercu Buana Bekasi Pemrograman Web Genap 2019 </p> </body> </html> |
File latihan16.5_update.php
include "db.php"; $id = $_POST['id']; $nama = $_POST['nama']; $alamat = $_POST['alamat']; $email = $_POST ['email']; $no_hp = $_POST ['no_hp']; |
$update = "UPDATE data_tamu SET nama='$nama', alamat='$alamat', email='$email', no_hp='$no_hp' WHERE id=$id"; mysqli_query ($conn, $update); header('location:./latihan16.5.php'); |
<?php include "db.php"; $id = $_POST['id']; $nama = $_POST['nama']; $alamat = $_POST['alamat']; $email = $_POST ['email']; $no_hp = $_POST ['no_hp']; //// debug isi value form // echo "$id <br>"; // echo "$nama <br>"; // echo "$alamat <br>"; // echo "$email <br>"; // echo "$no_hp <br>"; $update = "UPDATE data_tamu SET nama='$nama', alamat='$alamat', email='$email', no_hp='$no_hp' WHERE id=$id"; mysqli_query ($conn, $update); //// debug error query // if (mysqli_query($conn, $update)){ // echo "Data Berhasil di tambahkan"; // } // else { // echo "Gagal mengupdate data : " . mysqli_error($conn); // } header('location:./latihan16.5.php'); ?> |