Sabtu, 22 Februari 2014

Tutorial Create, Read, Update, Delete (CRUD) dengan PHP MySQL PART 2 (Input & Read)

Sekarang kita akan membuat fungsi Input data dan Read data.
Langsung saja sob tanpa basa basi.
Input.php
<?php 
include 'koneksi.php';
$query=mysql_query('SELECT * FROM jurusan');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Input Mahasiswa</title>
</head>

<body>
<h1 align="center">Input Mahasiswa</h1>
<fieldset style="width: 50%; margin: auto;">
    <legend>Form Input </legend>
     
    <form action="input_data.php" method="post">
     <p>
            Nip 
 
            <input type="text" name="nip" required />
      </p>
        <p>
            Nama 
 
            <input type="text" name="nama" required />
      </p>
         
        <p>
            Tempat Tanggal Lahir 
    <input type="text" name="tempat" required />
          <select name="tgl" required>
           <?php
                            for ($a=1;$a<=31;$a++)
                                {
                                    echo "<option value=".$a.">".$a."</option>";
                                }
                    ?>
          </select> <select name="bln" required>
            <option value="01">January</option>
            <option value="02">February</option>
            <option value="03">Maret</option>
            <option value="04">April</option>
            <option value="05">Mei</option>
            <option value="06">Juni</option>
            <option value="07">July</option>
            <option value="08">Agustus</option>
            <option value="09">September</option>
            <option value="10">Oktober</option>
            <option value="11">November</option>
            <option value="12">Desember</option>
          </select> 
          
          <select name="thn" required>
           <?php
                        $now=date("Y")-10;
                     
                            for ($a=$now;$a>=1970;$a--)
                                {
                                    echo "<option value=".$a." >".$a."</option>";
                                }
                    ?>
          </select>
        </p>
         
        <p>
            Jurusan      
             <select name="jurusan" required>
             <?php
    while ($row=mysql_fetch_assoc($query)){
       ?>
   <option value="<?php echo $row['id_jurusan']?>"><?php echo $row['nama_jurusan'] ?> </option>
            <?php } ?>
             </select>
        </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">≪ Tabel Mahasiswa</a></center>
</body>
</html>
Sekarang membuat fungsi untuk menyimpan ke database.
input_data.php
<?php
include 'koneksi.php';
$nip = $_POST['nip'];
$nama = $_POST['nama'];
$tempat=$_POST['tempat'];
$tgl_lahir=$_POST['thn'].'-'.$_POST['bln'].'-'.$_POST['tgl'];
$jurusan = $_POST['jurusan'];
$query=mysql_query ("INSERT INTO mahasiswa (nip, nama,tempat,tgl_lahir, id_jurusan)
VALUES ('$nip','$nama','$tempat','$tgl_lahir','$jurusan')",$connection)or die (mysql_error());
if($query) {
?>
<script language="javascript">
document.location='index.php'</script>
<?php
}
?>
Sekarang membuat tabel yang berfungsi melihat data dari database.
index.php
<?php 
include 'koneksi.php';
$query=mysql_query("SELECT *
FROM `mahasiswa`
LEFT JOIN jurusan ON mahasiswa.id_jurusan = jurusan.id_jurusan
ORDER BY nama ASC");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mahasiswa</title>
</head>

<body>
<h1 align="center">Data Mahasiswa</h1>
<center><a href="input.php" >INSERT DATA </a></center>
<table width="70%" border="1" align="center">
  <tr>
    <td>No</td>
    <td>Nip</td>
    <td>Nama</td>
    <td>TTL</td>
    <td>Jurusan</td>
    <td></td>
    
  </tr>
  <?php 
  $no=1;
  while ($row=mysql_fetch_assoc($query)){
   
   ?>
  <tr>
    <td><?php echo $no++ ?></td>
    <td><?php echo $row['nip'] ?></td>
    <td><?php echo $row['nama'] ?></td>
    <td><?php echo $row['tempat'].', '.date('d/m/Y',strtotime($row['tgl_lahir']))?></td>
    <td><?php echo $row['nama_jurusan'] ?></td>
    <td><a href="edit.php?id=<?php echo $row['nip'] ?>">Edit</a> || <a href="delete.php?id=<?php echo $row['nip'] ?>" onclick="return confirm('Apakah anda yakin akan menghapus data ini?')">Delete </a></td>
    
  </tr>
  <?php } ?>
</table>

</body>
</html>
Sekarang tinggal di tes aplikasi yang kita buat ,ketik localhost/mahasiswa di addres bar browser anda.
Kalau berhasil akan tampil seperti ini
Halaman Index.php
Halaman input.php
Segitu dulu tutorial dari saya, selanjutnya akan membahas tentang Edit dan Delete

1 komentar: