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

PHP Crud

The document describes the steps to create a basic CRUD (create, read, update, delete) application in PHP and MySQL. It involves creating a database and table, connecting to the database, and then building out PHP files to handle each CRUD function - an index page to view records, forms to input and edit data, and PHP scripts to save, update, and delete records from the database table. Code examples are provided for each step to demonstrate how to build the basic CRUD application.

Uploaded by

Ione Arkadianto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

PHP Crud

The document describes the steps to create a basic CRUD (create, read, update, delete) application in PHP and MySQL. It involves creating a database and table, connecting to the database, and then building out PHP files to handle each CRUD function - an index page to view records, forms to input and edit data, and PHP scripts to save, update, and delete records from the database table. Code examples are provided for each step to demonstrate how to build the basic CRUD application.

Uploaded by

Ione Arkadianto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Kita buat dulu database nya di phpmyadmin, contohnya seperti gambar di bawah ini, beri
nama Database dengan biodata, kemudian tabel beri nama tabel_biodata :

2. Kita buat "connect.php" :

<?php
$dsn  = "mysql:dbname=biodata;host=localhost";
$user = "root";
$pass = "";

try {
    $dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
    echo "Koneksi ke database gagal: ".$e->getMessage();
}
?>

3. Kita buat halaman "index.php" :

<?php
include 'connect.php';
?>
<style>
    tbody > tr:nth-child(2n+1) > td, tbody > tr:nth-child(2n+1) > th {
        background-color: #ededed;
    }
    table{
        width: 70%;
        margin: auto;
        border-collapse: collapse;
        box-shadow: blue 3px;
    }
    thead tr {
        background-color: #80FFFE;
    }
</style>

<h1 align="center">Index CRUD | Ridwan Maulana</h1>

<center>[<a href="input.php">Input Biodata Kamu</a>]</center>


 <br>

<table border="1">
    <thead>
        <tr>
            <th>No</th>
            <th>Nama</th>
            <th>Jenis Kelamin</th>
            <th>Alamat</th>
            <th>No Hp</th>
            <th>Pilihan</th>
        </tr>
    </thead>
    
    <tbody>
    <?php
    $sql = "SELECT * FROM tabel_biodata ORDER BY id";
    $no  = 1;
    foreach ($dbh->query($sql) as $data) :
    ?>
        <tr>
            <td><?php echo $no++; ?></td>
            <td><?php echo $data['nama'] ?></td>
            <td><?php echo $data['jenis_kelamin'] ?></td>
            <td><?php echo $data['alamat'] ?></td>
            <td><?php echo $data['no_hp'] ?></td>
            <td align="center">
                <a href="edit.php?id=<?php echo $data['id'] ?>">Edit |</a>
                  
                <a href="hapus.php?id=<?php echo $data['id'] ?>" onclick="return confirm('Anda
Yakin Ingin Menghapus Biodata Ini?')">Hapus</a>
            </td>
        </tr>
    <?php
    endforeach;
    ?>
    </tbody>
</table> 

4. Sekarang kita buat "input.php" :

<h1 align="center">Input Biodata</h1>


<fieldset style="width: 50%; margin: auto;">
    <legend>Form Input Biodata</legend>
    
    <form action="simpan.php" method="post">
        <p>
            Nama Lengkap

            <input type="text" name="nama" required />


        </p>
        
        <p>
            Jenis Kelamin

            <input type="radio" name="jenis_kelamin" value="Laki-Laki" id="laki-laki" /><label


for="laki-laki">Laki-Laki</label>
            <input type="radio" name="jenis_kelamin" value="Perempuan" id="perempuan"
/><label for="perempuan">Perempuan</label>
        </p>
        
        <p>
            Alamat Lengkap

            <textarea name="alamat" cols="50" required></textarea>


        </p>
        
        <p>
            Nomor Handphone

            <input type="text" name="no_hp" required />


        </p>
        
        <p>
            <input type="submit" value="Simpan" />
            <input type="reset" value="Reset" onclick="return confirm('hapus data yang telah
diinput?')">
        </p>
    </form>
</fieldset>

<center>[<a href="index.php">Kembali ke Tabel Biodata</a>]</center> 

5. Sekarang buat file "simpan.php" :

<?php
include 'connect.php';

if (isset($_POST)) {
    $sql = "INSERT INTO tabel_biodata VALUE ('', '$_POST[nama]', '$_POST[jenis_kelamin]',
'$_POST[alamat]', '$_POST[no_hp]')";
    $dbh->exec($sql);
}

header("location:index.php");
?> 

6. Kita buat file "edit.php" :

<?php
include 'connect.php';
if (isset($_GET['id'])) {
    $query = $dbh->query("SELECT * FROM tabel_biodata WHERE id = '$_GET[id]'");
    $data  = $query->fetch(PDO::FETCH_ASSOC);
} else {
    echo "ID tidak tersedia!
<a href='index.php'>Kembali</a>";
    exit();
}

if ($data === false) {


    echo "Data tidak ditemukan!
<a href='index.php'>Kembali</a>";
    exit();
}
?>
<h1 align="center">Edit Biodata</h1>
<fieldset style="width: 50%; margin: auto;">
    <legend>Form Input Biodata</legend>
    
    <form action="update.php" method="post">
        <input type="hidden" name="id" value="<?php echo $data['id']; ?>" />
        <p>
            Nama Lengkap

            <input type="text" name="nama" required value="<?php echo $data['nama']; ?>"/>


        </p>
        
        <p>
            Jenis Kelamin

            <?php if ($data['jenis_kelamin'] === "Laki-Laki") : ?>


            <input type="radio" name="jenis_kelamin" value="Laki-Laki" id="laki-laki" checked
/><label for="laki-laki">Laki-Laki</label>
            <input type="radio" name="jenis_kelamin" value="Perempuan" id="perempuan"
/><label for="perempuan">Perempuan</label>
            <?php else : ?>
            <input type="radio" name="jenis_kelamin" value="Laki-Laki" id="laki-laki" /><label
for="laki-laki">Laki-Laki</label>
            <input type="radio" name="jenis_kelamin" value="Perempuan" id="perempuan"
checked /><label for="perempuan">Perempuan</label>
            <?php endif; ?>
        </p>
        
        <p>
            Alamat Lengkap

            <textarea name="alamat" cols="50" required><?php echo $data['alamat']; ?></textarea>


        </p>
        
        <p>
            Nomor Handphone

            <input type="text" name="no_hp" required value="<?php echo $data['no_hp']; ?>" />
        </p>
        
        <p>
            <input type="submit" value="Simpan" />
            <input type="reset" value="Reset" onclick="return confirm('Anda Yakin akan Menginput
Data Yang Sudah Diinput?')">
        </p>
    </form>
</fieldset>

<center><a href="index.php">Kembali Tabel Biodata</a></center> 

7. Buat file "hapus.php" :

<?php
include 'connect.php';
if (isset($_GET['id'])) {
    $dbh->exec("DELETE FROM tabel_biodata WHERE id = '$_GET[id]'");
}
header("location:index.php")
?> 

8. Yang terakhir buat "update.php" :

<?php
include 'connect.php';

if (isset($_POST)) {
    $sql = "UPDATE tabel_biodata SET nama = '$_POST[nama]',
                                     jenis_kelamin = '$_POST[jenis_kelamin]',
                                     alamat = '$_POST[alamat]',
                                     no_hp  = '$_POST[no_hp]'
                                 WHERE id = '$_POST[id]' ";
    $dbh->exec($sql);
}

header("location:index.php");
?> 

Selesai !
Tampilan CRUD yang Anda buat

CREATE TABLE `user` (


  `id` int(11) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `nama` varchar(32) NOT NULL,
  `email` varchar(32) NOT NULL,
  `level` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `tabel_biodata` (

`id` varchar(20) NOT NULL,

`nama` varchar(60) default NULL,

`jenis_kelamin` varchar(255) default NULL,

`alamat` varchar(40) default NULL,

`no_hp` varchar(12) default NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tabel_biodata`

--

INSERT INTO `tabel_biodata` (`id`, `nama`, `jenis_kelamin`, `alamat`, `no_hp`) VALUES

('1', 'Bagus Sekali', 'Laki-Laki', 'Sepaku aja', '567888888');

You might also like