CRUD Data Dari Database PHP MYSQLI
Assalammm'alaikum WR.WB.
kembali lagi bersama saya gunawan Dwi Pangga, nah tanpa membuang banyak waktu langsung saja saya akn berithu teman-teman bagaimana cara membuat CRUD Data Dari Database PHP MYSQLI
Nah disini kita ada 4 kode yang harus kita siapkan terlebih dahulu
yang pertama yaitu ; index.php/tambah.php/delete.php &edit.php
berikut kode yang pertama :
<?php
$k = mysqli_connect("localhost","root","","rpl");
$user = $k->query("SELECT * FROM user");
?>
<!DOCTYPE html>
<html>
<head>
<title>latihan 11</title>
</head>
<body>
<a href='tambah.php'>Tambah</a>
<table border='1' width="500">
<tr>
<td>No</td>
<td>Nama</td>
<td>Alamat</td>
<td>Username</td>
<td>Password</td>
<td>Action</td>
</tr>
<?php
$no=1;
while($u=mysqli_fetch_array($user)){
?>
<tr>
<td><?php echo $no; $no++; ?></td>
<td><?php echo $u['nama'];?></td>
<td><?php echo $u['alamat'];?></td>
<td><?php echo $u['username'];?></td>
<td><?php echo $u['password'];?></td>
<td>
<a href="delete.php?id=<?php echo $u['id'];?>">Delete</a>
<a href="edit.php?id=<?php echo $u['id'];?>">Edit</a>
</td>
</tr>
<?php } ?>
</table>
<br />
<br />
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
yang kedua
<?php
$k = mysqli_connect("localhost","root","","rpl");
if(isset($_POST['nama'])){
extract($_POST);
$k->query("INSERT INTO user VAlUES('','$nama','$alamat','$username','$password')");
header("location.index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah Data</title>
</head>
<body>
<form method="post">
<input type="text" name="nama" placeholder="Nama"><br />
<textarea name="alamat" placeholder="Alamat"></textarea><br />
<input type="text" name="username" placeholder="Username"><br />
<input type="password" name="password" name="password" placeholder="Password">
<br />
<button>Selesai</button>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
yang ke tiga
<?php
$id = $_GET['id'];
$k = mysqli_connect("localhost","root","","rpl");
$k->query("DELETE FROM user WHERE id='$id'");
header("location:index.php");
?>
--------------------------------------------------------------------------------------------------------------------------
yang ke empat
<?php
$id = $_GET['id'];
$k = mysqli_connect("localhost","root","","rpl");
$user = $k->query("SELECT * FROM user WHERE id='$id'");
$u = mysqli_fetch_array($user);
if(isset($_POST['nama'])){
extract($_POST);
$k->query("UPDATE user SET nama='$nama',alamat='$alamat',username='$username',password='$password' WHERE id='$id'");
header("location.index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Php</title>
</head>
<body>
<form method="post">
<input type="text" name="nama" value="<?php echo $u['nama'];?>" placeholder="Nama"><br />
<textarea name="alamat" placeholder="Alamat"><?php echo $u['alamat'];?></textarea><br />
<input type="text" name="username" value="<?php echo $u['username'];?>" placeholder="Username"><br />
<input type="password" name="password" value="<?php echo $u['password'];?>" placeholder="Password"><br />
<button>Selesai</button>
</form>
</body>
</html>
selamat mengerjakan
Komentar
Posting Komentar