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

OOP PHP MYSQLI Sekolah

The document discusses an OOP PHP and MySQL student database project that includes classes to connect to a database and perform CRUD operations on a student table, with pages to display, add, edit, and delete student records from the table. It includes a database class to connect to MySQL, a student class to define methods for data operations, and interface pages that include, edit, and process student data using the classes.

Uploaded by

RIvå MâŭLäna
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)
98 views

OOP PHP MYSQLI Sekolah

The document discusses an OOP PHP and MySQL student database project that includes classes to connect to a database and perform CRUD operations on a student table, with pages to display, add, edit, and delete student records from the table. It includes a database class to connect to MySQL, a student class to define methods for data operations, and interface pages that include, edit, and process student data using the classes.

Uploaded by

RIvå MâŭLäna
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/ 5

OOP PHP & MYSQLI

Database : db_sekolah
Tabel : tb_mahasiswa
Field Type(Length) Ket
NIM Int(11) Primary key
Nama Varchar(35)
Alamat text
Isi data :
NIM Nama Alamat
201801 Agung Garut
201802 Arap Tasikmalaya
201803 Chandra Cirebon

index.php
<h1>CRUD OOP PHP</h1>
<h2>DATA SEKOLAH</h2>

<?php
include "menu.php";

echo "<p>";
include "isi.php";
echo "</p>";
?>
menu.php
|<a href=index.php>Home</a>
|<a href=index.php?mn=tm>Mahasiswa</a>
isi.php

<?php
extract($_GET);
if(isset($mn))
{
if($mn=="tm") include "tampil_mahasiswa.php";
else if($mn=="pm") include "proses_mahasiswa.php";
else if($mn=="im") include "input_mahasiswa.php";
else if($mn=="em") include "edit_mahasiswa.php";
}
?>

database.php
<?php

class database{

var $host = "localhost";


var $user = "root";
var $pass = "";
var $db = "db_sekolah";

function __construct(){
mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db);
}

?>
proses_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();

extract($_POST);
$aksi = $_GET['aksi'];

if($aksi == "tambah"){
$mhs->input($a,$b,$c);
header("location:index.php?mn=tm");
}

elseif($aksi == "hapus"){
$mhs->hapus($_GET['id']);
header("location:index.php?mn=tm");
}

elseif($aksi == "update"){
$mhs->update($a,$b,$c);
header("location:index.php?mn=tm");
}
?>

class_mahasiswa.php
<?php
include 'database.php';
class mahasiswa extends database
{
function __construct(){
parent::__construct();
}
function tampil_data(){
$data = mysql_query("select * from tb_mahasiswa");
while($x = mysql_fetch_array($data)){
$hasil[] = $x;
}
return $hasil;
}

function input($nim,$nama,$alamat){
mysql_query("insert into tb_mahasiswa
values('$nim','$nama','$alamat')");
}

function hapus($id){
mysql_query("delete from tb_mahasiswa where nim='$id'");
}

function edit($id){
$data = mysql_query("select * from tb_mahasiswa where
nim='$id'");
while($d = mysql_fetch_array($data)){
$hasil[] = $d;
}
return $hasil;
}

function update($id,$nama,$alamat){
mysql_query("update tb_mahasiswa set nama='$nama',
alamat='$alamat' where nim='$id'");
}
}
?>
tampil_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();
?>
<a href="index.php?mn=im">+ Input Data</a>
<table border="1">
<tr>
<th>No</th>
<th>Nama</th>
<th>Alamat</th>
<th>Opsi</th>
</tr>
<?php
$no = 1;
foreach($mhs->tampil_data() as $x){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $x['nim']; ?></td>
<td><?php echo $x['nama']; ?></td>
<td><?php echo $x['alamat']; ?></td>
<td>
<a href="index.php?mn=em&id=<?php echo $x['nim']; ?
>&aksi=edit">Edit</a>
<a href="index.php?mn=pm&id=<?php echo $x['nim']; ?
>&aksi=hapus">Hapus</a>
</td>
</tr>
<?php
}
?>
</table>

input_mahasiswa.php

<form action="index.php?mn=pm&aksi=tambah" method="post">

<table>
<tr>
<td>NIM</td>
<td><input type="text" name="a"></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="b"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="c"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Simpan"></td>
</tr>
</table>
</form>
edit_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();
?>

<form action="index.php?mn=pm&aksi=update" method="post">


<?php
foreach($mhs->edit($_GET['id']) as $y){
?>
<table>
<tr>
<td>Nama</td>
<td>
<input type="hidden" name="a" value="<?php echo
$y['nim'] ?>">
<input type="text" name="b" value="<?php echo
$y['nama'] ?>">
</td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="c" value="<?php echo
$y['alamat'] ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Simpan"></td>
</tr>
</table>
<?php } ?>
</form>

You might also like