0% found this document useful (0 votes)
153 views3 pages

Membuat Crud Dengan Oop PHP Dan Mysql

This document discusses creating CRUD (create, read, update, delete) operations in PHP using an OOP (object-oriented programming) approach with a MySQL database. It includes a database class to connect to and query the database. It also includes PHP pages for displaying, adding, editing, and processing data. The database class contains methods for retrieving, inserting, deleting, updating, and editing user data in the database. The other pages include forms and queries to allow adding, viewing, and modifying user records through the database class methods.

Uploaded by

room karoke
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)
153 views3 pages

Membuat Crud Dengan Oop PHP Dan Mysql

This document discusses creating CRUD (create, read, update, delete) operations in PHP using an OOP (object-oriented programming) approach with a MySQL database. It includes a database class to connect to and query the database. It also includes PHP pages for displaying, adding, editing, and processing data. The database class contains methods for retrieving, inserting, deleting, updating, and editing user data in the database. The other pages include forms and queries to allow adding, viewing, and modifying user records through the database class methods.

Uploaded by

room karoke
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/ 3

Membuat CRUD Dengan OOP PHP Dan MySQL

database.php
<?php 

class database{

    var $host = "localhost";
    var $uname = "root";
    var $pass = "";
    var $db = "malasngoding";

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

    function tampil_data(){
        $data = mysql_query("select * from user");
        while($d = mysql_fetch_array($data)){
            $hasil[] = $d;
        }
        return $hasil;

    }

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

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

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

    function update($id,$nama,$alamat,$usia){
        mysql_query("update user set nama='$nama', alamat='$alamat', usia='$usia' where id='$id'
");
    }

?>
tampil.php

<?php 
include 'database.php';
$db = new database();
?>
<h1>CRUD OOP PHP</h1>
<h2>WWW.MALASNGODING.COM</h2>
<h3>Data User</h3>

<a href="input.php">Input Data</a>
<table border="1">
    <tr>
        <th>No</th>
        <th>Nama</th>
        <th>Alamat</th>
        <th>Usia</th>
        <th>Opsi</th>
    </tr>
    <?php
    $no = 1;
    foreach($db->tampil_data() as $x){
    ?>
    <tr>
        <td><?php echo $no++; ?></td>
        <td><?php echo $x['nama']; ?></td>
        <td><?php echo $x['alamat']; ?></td>
        <td><?php echo $x['usia']; ?></td>
        <td>
            <a href="edit.php?id=<?php echo $x['id']; ?>&aksi=edit">Edit</a>
            <a href="proses.php?id=<?php echo $x['id']; ?>&aksi=hapus">Hapus</a>            
        </td>
    </tr>
    <?php 
    }
    ?>
</table>

edit.php

<?php 
include 'database.php';
$db = new database();
?>

<h1>CRUD OOP PHP</h1>
<h2>WWW.MALASNGODING.COM</h2>
<h3>Edit Data User</h3>

<form action="proses.php?aksi=update" method="post">
<?php
foreach($db->edit($_GET['id']) as $d){
?>
<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>Alamat</td>
        <td><input type="text" name="alamat" value="<?php echo $d['alamat'] ?>"></td>
    </tr>
    <tr>
        <td>Usia</td>
        <td><input type="text" name="usia" value="<?php echo $d['usia'] ?>"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Simpan"></td>
    </tr>
</table>
<?php } ?>
</form>

input.php
<h1>CRUD OOP PHP</h1>
<h2>WWW.MALASNGODING.COM</h2>
<h3>Tambah Data User</h3>

<form action="proses.php?aksi=tambah" method="post">
    
<table>
    <tr>
        <td>Nama</td>
        <td><input type="text" name="nama"></td>
    </tr>
    <tr>
        <td>Alamat</td>
        <td><input type="text" name="alamat"></td>
    </tr>
    <tr>
        <td>Usia</td>
        <td><input type="text" name="usia"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Simpan"></td>
    </tr>
</table>
</form>

proses.php
<?php 
include 'database.php';
$db = new database();

$aksi = $_GET['aksi'];
 if($aksi == "tambah"){
    $db->input($_POST['nama'],$_POST['alamat'],$_POST['usia']);
    header("location:tampil.php");
 }elseif($aksi == "hapus"){     
    $db->hapus($_GET['id']);
    header("location:tampil.php");
 }elseif($aksi == "update"){
    $db->update($_POST['id'],$_POST['nama'],$_POST['alamat'],$_POST['usia']);
    header("location:tampil.php");
 }
?>

You might also like