Anda di halaman 1dari 8

TUGAS PEMROGRAMAN WEB

NAMA : YOSEP NARU

KELAS : Q

NIM : 2201010486

INSTITUT BISNIS DAN TEKNOLOGI INDONESIA

TAHUN AJARAN 2022/2023


1. Database SQL

2. Code untuk ,enampilkan halaman utama (homepage)

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

// Fetch all users data from database


$result = mysqli_query($koneksi, "SELECT * FROM daftar_kontak ORDER BY id
DESC");
?>

<html>
<head>
<title>daftar_kontak</title>
</head>

<body>
<a href="tambah kontak.php">Tambahkan Kontak Baru</a><br/><br/>

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

<tr>
<th>Id Kontak</th> <th>Nama Kontak</th> <th>Email</th>
<th>Telepon</th> <th>Update</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$user_data['id']."</td>";
echo "<td>".$user_data['nama_kontak']."</td>";
echo "<td>".$user_data['email']."</td>";
echo "<td>".$user_data['telepon']."</td>";
echo "<td><a href='edit kontak.php?id=$user_data[id]'>Edit Kontak</a>
| <a href='hapus.php?id=$user_data[id]'>hapus</a></td></tr>";
}
?>
</table>
</body>
</html>

• Output pada localhost

3. Code untuk menampilkan daftar kontak


4. <html>
5. <head>
6. <title>tambahkan kontak</title>
7. </head>
8.
9. <body>
10. <a href="index.php">daftar kontak</a>
11. <br/><br/>
12.
13. <form action="tambah kontak.php" method="post" name="form1">
14. <table width="25%" border="0">
15. <tr>
16. <td>id kontak</td>
17. <td><input type="text" name="id "></td>
18. </tr>
19. <tr>
20. <td>nama kontak</td>
21. <td><input type="text" name="nama kontak"></td>
22. </tr>
23. <tr>
24. <td>telepon</td>
25. <td><input type="text" name="telepon"></td>
26. </tr>
27. <tr>
28. <td>email</td>
29. <td><input type="text" name="email"></td>
30. </tr>
31. <tr>
32. <td></td>
33. <td><input type="submit" name="Submit"
value="Add"></td>
34. </tr>
35. </table>
36. </form>
37.
38. <?php
39.
40. // Check If form submitted, insert form data into users table.
41. if(isset($_POST['Submit'])) {
42. $id = $_POST['id'];
43. $nama = $_POST['nama_kontak'];
44. $telepon = $_POST['telepon'];
45. $email = $_POST['email'];
46.
47. // include database connection file
48. include_once("koneksi.php");
49.
50. // Insert user data into table
51. $result = mysqli_query($koneksi, "INSERT INTO
daftar_kontak(id,nama_kontak,telepon,email)
VALUES('$id','$nama','$telepon','$email')");
52.
53. // Show message when user addedid
54. echo "User added successfully. <a href='index.php'>lihat daftar
kontak</a>";
55. }
56. ?>
57. </body>
58. </html>

• Output pada localhost


• Output setelah dijalankan “add”

• Output pada SQL (phpmyadmin)

4. Edit kontak /mengupdate kontak berikut codenya

<?php
// include database connection file
include_once("koneksi.php");

// Check if form is submitted for user update, then redirect to homepage after
update
if(isset($_POST['update']))
{
$id = $_POST['id'];
$nama=$_POST['nama_kontak'];
$email=$_POST['email'];
$telepon=$_POST['telepon'];

// update user data


$result = mysqli_query($koneksi, "UPDATE daftar_kontak SET
nama_kontak='$nama',telepon='$telepon',email='$email' 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($koneksi, "SELECT * FROM daftar_kontak WHERE id=$id");

while($user_data = mysqli_fetch_array($result))
{
$id= $user_data ['id'];
$nama= $user_data ['nama_kontak'];
$email= $user_data['email'];
$telepon= $user_data['telepon'];
}
?>
<html>
<head>
<title>Edit Daftar Kontak</title>
</head>

<body>
<a href="index.php">Daftar Kontak</a>
<br/><br/>

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


<table border="0">
<tr>
<td>Id</td>
<td><input type="text" name="id" value=<?php echo $id;?>></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama kontak" value=<?php echo
$nama;?>></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" value=<?php echo
$email;?>></td>
</tr>
<tr>
<td>Telepon</td>
<td><input type="text" name="email" value=<?php echo
$telepon;?>></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>

• Sebelum di update di localhost

• Setelah diupdate di localhost

• Output di SQL PHPmyadmin

5. menghapus kontak (dengan perintah DELETE)

• Sebelum dihapus

• Setelah dihapus ( data kontak dengan nama kontak yosep telah dihapus dari daftar kontak)
• Output pada mySQL PHPmyadmin

Anda mungkin juga menyukai