0% found this document useful (0 votes)
26 views

Materi Databases Dan PHP

The document describes a student data management system using PHP and MySQL. It includes code for connecting to the database, displaying data in a table, adding new data, editing existing data, and deleting data. The main pages are index.php which shows all student data, tambah.php for adding new students, edit.php for editing a student, and hapus.php for deleting a student. Functions are included to connect to the database, query and manipulate the data, and redirect between pages.

Uploaded by

Anis Sulastri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Materi Databases Dan PHP

The document describes a student data management system using PHP and MySQL. It includes code for connecting to the database, displaying data in a table, adding new data, editing existing data, and deleting data. The main pages are index.php which shows all student data, tambah.php for adding new students, edit.php for editing a student, and hapus.php for deleting a student. Functions are included to connect to the database, query and manipulate the data, and redirect between pages.

Uploaded by

Anis Sulastri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MENAMPILKAN DATA

KONEKSI.PHP
<?php
$koneksi = mysqli_connect("localhost","root","","akademik");

// Check connection
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}

?>

INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<title>CRUD PHP dan MySQLi - WWW.MALASNGODING.COM</title>
</head>
<body>

<h2>CRUD DATA MAHASISWA - WWW.MALASNGODING.COM</h2>


<br/>
<a href="tambah.php">+ TAMBAH MAHASISWA</a>
<br/>
<br/>
<table border="1">
<tr>
<th>NO</th>
<th>Nama</th>
<th>NIM</th>
<th>Alamat</th>
<th>OPSI</th>
</tr>
<?php
include 'koneksi.php';
$no = 1;
$data = mysqli_query($koneksi,"select * from mahasiswa");
while($d = mysqli_fetch_array($data)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['nama']; ?></td>
<td><?php echo $d['nim']; ?></td>
<td><?php echo $d['alamat']; ?></td>
<td>
<a href="edit.php?id=<?php echo $d['id']; ?
>">EDIT</a>
<a href="hapus.php?id=<?php echo $d['id']; ?
>">HAPUS</a>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>

INPUT DATA
TAMBAH.PHP
<!DOCTYPE html>
<html>
<head>
<title>CRUD PHP dan MySQLi - WWW.MALASNGODING.COM</title>
</head>
<body>

<h2>CRUD DATA MAHASISWA - WWW.MALASNGODING.COM</h2>


<br/>
<a href="index.php">KEMBALI</a>
<br/>
<br/>
<h3>TAMBAH DATA MAHASISWA</h3>
<form method="post" action="tambah_aksi.php">
<table>
<tr>
<td>Nama</td>
<td><input type="text" name="nama"></td>
</tr>
<tr>
<td>NIM</td>
<td><input type="number" name="nim"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SIMPAN"></td>
</tr>
</table>
</form>
</body>
</html>

TAMBAH_AKSI.PHP
<?php
// koneksi database
include 'koneksi.php';

// menangkap data yang di kirim dari form


$nama = $_POST['nama'];
$nim = $_POST['nim'];
$alamat = $_POST['alamat'];

// menginput data ke database


mysqli_query($koneksi,"insert into mahasiswa
values('','$nama','$nim','$alamat')");

// mengalihkan halaman kembali ke index.php


header("location:index.php");

?>
UPDATE DATA
EDIT.PHP
<!DOCTYPE html>
<html>
<head>
<title>CRUD PHP dan MySQLi - WWW.MALASNGODING.COM</title>
</head>
<body>

<h2>CRUD DATA MAHASISWA - WWW.MALASNGODING.COM</h2>


<br/>
<a href="index.php">KEMBALI</a>
<br/>
<br/>
<h3>EDIT DATA MAHASISWA</h3>

<?php
include 'koneksi.php';
$id = $_GET['id'];
$data = mysqli_query($koneksi,"select * from mahasiswa where id='$id'");
while($d = mysqli_fetch_array($data)){
?>
<form method="post" action="update.php">
<table>
<tr>
<td>Nama</td>
<td>
<input type="hidden" name="id" value="<?
php echo $d['id']; ?>">
<input type="text" name="nama" value="<?
php echo $d['nama']; ?>">
</td>
</tr>
<tr>
<td>NIM</td>
<td><input type="number" name="nim"
value="<?php echo $d['nim']; ?>"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" value="<?
php echo $d['alamat']; ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SIMPAN"></td>
</tr>
</table>
</form>
<?php
}
?>

</body>
</html>
Setelah yang di atas lalu di edit,seperti yang dibawah;
$id = $_GET['id'];
$data = mysqli_query($koneksi,"select * from mahasiswa where id='$id'");
while($d = mysqli_fetch_array($data)){

kemudian kita tampilkan dalam form


<form method="post" action="update.php">
<table>
<tr>
<td>Nama</td>
<td>
<input type="hidden" name="id" value="<?php echo
$d['id']; ?>">
<input type="text" name="nama" value="<?php echo
$d['nama']; ?>">
</td>
</tr>
<tr>
<td>NIM</td>
<td><input type="number" name="nim" value="<?php echo
$d['nim']; ?>"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" value="<?php echo
$d['alamat']; ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SIMPAN"></td>
</tr>
</table>
</form>
UPDATE.PHP
<?php
// koneksi database
include 'koneksi.php';
// menangkap data yang di kirim dari form
$id = $_POST['id'];
$nama = $_POST['nama'];
$nim = $_POST['nim'];
$alamat = $_POST['alamat'];

// update data ke database


mysqli_query($koneksi,"update mahasiswa set nama='$nama', nim='$nim',
alamat='$alamat' where id='$id'");

// mengalihkan halaman kembali ke index.php


header("location:index.php");

?>
HAPUS DATA
HAPUS.PHP
<?php
// koneksi database
include 'koneksi.php';

// menangkap data id yang di kirim dari url


$id = $_GET['id'];
// menghapus data dari database
mysqli_query($koneksi,"delete from mahasiswa where id='$id'");

// mengalihkan halaman kembali ke index.php


header("location:index.php");

?>

You might also like