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

PHP

The documents contain PHP code for a student data CRUD (create, read, update, delete) application. koneksi.php establishes a connection to a MySQL database. Index.php displays student data from the database in a table and includes links to add, edit and delete data. Tambah.php and Tambah_aksi.php contain forms and code to insert new student records. Edit.php displays a form populated with existing data to update a record and Update.php code updates the record. Hapus.php deletes a record based on the ID passed via a URL parameter.

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

PHP

The documents contain PHP code for a student data CRUD (create, read, update, delete) application. koneksi.php establishes a connection to a MySQL database. Index.php displays student data from the database in a table and includes links to add, edit and delete data. Tambah.php and Tambah_aksi.php contain forms and code to insert new student records. Edit.php displays a form populated with existing data to update a record and Update.php code updates the record. Hapus.php deletes a record based on the ID passed via a URL parameter.

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/ 20

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>
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");

?>
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>

$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>

<input type="hidden" name="id" value="<?php echo $d['id']; ?>">


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.phphap
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");

?>
S

You might also like