Sunday, February 1, 2015

Crud Dengan PHP

1.      Halaman login
Sintak bahasa pemrograman:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
            <title>Login</title>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <meta name="generator" content="Bluefish 2.2.3" />
            <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<?php
            $init=$_GET['init'];
            switch($init)
            {
                     
   case 1:
                                    $msg="<p align='center'>Login Gagal, Mohon Periksa Username dan Password anda dengan benar</p>";
                                    break;
                        case 2:
                                    $msg="<p align='center'>Anda berhasil Logout, Terima Kasih</p>";
                                    break;
                        case 3:
                                    $msg="<p align='center'>Maaf Anda belum Login, silahkan login terlebih dahulu</p>";
                                    break;
            }
           
            ?>
            <h3>Halaman Login</h3>
            <?php echo $msg?>
            <div id='content'>
            <form action='login_check.php' method='post'>
            <table align'center'>ss
                                    <td><label>Username :</label></td>
                                    <td><input type='text' name='username' placeholder='username'></td>
                        </tr>
                        <tr>     
                                    <td><label>Password :</label></td>
                                    <td><input type='password' name='password' placeholder='password'></td>
                        </tr>
                        <tr>
                        <td></td>
                                    <td>
                                                <input type='reset' name='reset' value='Reset'>
                                                <input type='submit' name='submit' value='Login'>
                                    </td>
                        </tr>
                        </table>
                        </form>
                        </div>
            </div>
</body>
</html>

2.      Koneksi Database ‘koneksi.php’
<?php
$con=mysql_connect('localhost','root','dian');
if(!$con)
            {
                        die("mysql belum aktif");
            }
if(!mysql_select_db('web',$con)) die("database salah");

else
{
           
}
?>

3.      Login check

<?php
include "koneksi.php";
session_start();
$username=$_POST['username'];
$password=$_POST['password'];
$check=0;

$sql="select * from admin where username='$username' and password='$password'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
           
if($username == "" || $password == "")
            {
                        header("location:index.php?init=1");
            }
else
            {
            if($password == "$data[password]")
            {
                        $_SESSION['id']=$data['id_admin'];
                        $_SESSION['nama']=$data['nama_admin'];
                        $_SESSION['username']=$data['username'];
                        $_SESSION['password']=$data['password'];
                        header("location:home.php");
            }
            else
            {
                        header("location:index.php?init=1");
            }
            }
?>


4.      page.php

<?php
session_start();
include ("koneksi.php");
$user=$_SESSION['nama'];
switch($_GET[page]){
            default://=====================default
            echo "
            <p align='center'>Welcome Administrator</p>";
            break; //======================Default
            case "user": //===================user
                        $sql=mysql_query("select*from admin");
            echo"
            <table align='center' width='50%' border='1'>
            <tr>
                        <th>Nama</th>
                        <th>Username</th>
                        <th>Password</th>
                        </tr>";
                        while($data=mysql_fetch_array($sql))
                        {
                                    echo"
                                    <tr>
                                    <td>$data[nama_admin]</td>
                                    <td>$data[username]</td>
                                    <td>$data[password]</td>
                                    </tr>";
                        }
                        echo"</table>";
            break; //=================break user
//pembuatan page kategori
            case "kategori": //===================kategori
                        $sql=mysql_query("select*from kategori");
            echo"
            <table align='center' width='50%' border='1'>
            <tr>
                        <th>Kategori</th>
                        <th>Aktif</th>
                        <th colspan='2'>Aksi</th>
                       
                        </tr>";
                        while($data=mysql_fetch_array($sql))
                        {
                                    echo"
                                    <tr>
                                    <td align='center'>$data[kategori]</td>
                                    <td align='center'>$data[aktif]</td>
                                    <td algin='center'><a href='home.php?page=editkategori&id=$data[id_kategori]'>Ubah</a></td>
                                    <td algin='center'><a href='home.php?page=hapuskategori&id=$data[id_kategori]'>Hapus</a></td>
                        </tr>";
                        }
                        echo"</table>";
            break; //=================break kategori
case "hapuskategori";
$id=$_GET['id'];
$h=mysql_query("delete from kategori where id_kategori='$id'");
if ($h)
{
            echo "<p align='center'>Data Berhasil di Hapus</p>";
            echo "<p align='center'><a href='home.php?page=kategori'>Kembali</a></p>";
}
else
{
            echo "<p align='center'>Data Gagal di Hapus</p>";
            echo "<p align='center'><a href='home.php?page=kategori'>Kembali</a></p>";
}
case"editkategori":
$id=$_GET['id'];
$h=mysql_query("select*from kategori where id_kategori='$id'");
$data=mysql_fetch_array($h);
echo"
<p align='center'> Edit Kategori </p>
                        <form action='home.php?page=ubahkategori' method='post'>
                                    <input type='hidden' name='id' value='$id'>
                                    <input type ='text' name='kategori' value='$data[kategori]'> <br>";
                                    if ($data[aktif]=='Y')
                                    {
                                                echo"
                                    <input type ='radio' name='aktif' value='Y' checked>Y
                                    <input type ='radio' name='aktif' value='T' >T ";
                        }
                        else
                        {
                                    echo"
                                    <input type ='radio' name='aktif' value='Y' >Y
                                    <input type ='radio' name='aktif' value='T' checked> T";
                        }
                                    echo"
                                    <br>
                                                <input type ='button' onclick='self.history.back()' value='Cancel'>
                                                <input type ='submit' name='submit' value='Simpan'>
                        </form>";
                        break;             
break;
case "ubahkategori":
$id=$_REQUEST['id'];
$kategori=$_REQUEST['kategori'];
$aktif=$_REQUEST['aktif'];
if($kategori=='')
{
            echo "<p align='center'>Kategori tidak boleh kosong</p>";
            echo "<p align='center'><a href='home.php?page=editkategori&id=$id'>Kembali</a></p>";
}
else
{
$h=mysql_query("update kategori set kategori='$kategori',aktif='$aktif' where id_kategori='$id'");
if ($h)
{
            echo "<p align='center'>Data Berhasil di Ubah</p>";
            echo "<p align='center'><a href='home.php?page=kategori'>Kembali</a></p>";
}
else
{
            echo "<p align='center'>Data Gagal di Ubah</p>";
            echo "<p align='center'><a href='home.php?page=kategori'>Kembali</a></p>";
}
}
break;
// tambah kategori
case"addkategori":
echo"
<p align='center'> Tambah Kategori </p>
                        <form action='home.php?page=tambahkategori' method='post'>
                                    <input type ='text' name='kategori' placeholder='kategori'> <br>
                                    <input type ='radio' name='aktif' value='Y' checked>Y
                                    <input type ='radio' name='aktif' value='T' >T
                                                <input type ='reset' name='reset' value='Reset'>
                                                <input type ='submit' name='submit' value='simpan'>
                        </form>";
                        break;                                     
                       
case "tambahkategori":
            $kategori=$_POST['kategori'];
            $aktif=$_POST['aktif'];
if ($kategori=='')
{
            echo"<p align='center'>Kategori Tidak Boleh Null</p>";
            echo"<p align='center'><a href='home.php?page=addkategori'>Kembali</a></p>";
}                     
else
{         
            $sql=mysql_query("insert into kategori (kategori,aktif) values ('$kategori','$aktif')");
            if (sql) {
                        echo"<p align='center'>Berhasil<a href='home.php?page=kategori'>Kembali</a></p>";
            }
            else
            {
                        echo"<p align='center'>Gagal<a href='home.php?page=addkategori'>Ulangi</a></p>";
            }
}         
            break;

//page artikel

case"addartikel": //============================================add arikel
echo"
<p align='center'> Tambah Artikel </p>
                        <form action='home.php?page=tambahartikel' method='post'>
                                    <input type ='text' name='judul' placeholder='judul' size='50'> <br>
                        <select name='id_kategori'>
                                                <option value=''>Pilih kategori</option>";
                                                $sl=mysql_query("select*from kategori where aktif='Y'");
                                                while($d=mysql_fetch_array($sl)){
                                                            echo"<option value='$d[id_kategori]'>$d[kategori]</option>";
                                                }
                                                echo"
                                    </select>
                                    <textarea rows='15' name='isi' id='content'> </textarea>
                                    <script type='text/javascript'>
                                                var editor = CKEDITOR.replace('content');
                                                CKFinder.setupCKEditor(editor,'ckfinder/');
                                    </script>
                                    <br>
                                    <input type ='radio' name='aktif' value='Y' checked>Y
                                    <input type ='radio' name='aktif' value='N' >N
                                    <br>
                                                <input type ='reset' name='reset' value='Reset'>
                                                <input type ='submit' name='submit' value='simpan'>
                        </form>";
                        break; 
case "tambahartikel":                           //tambah artikel
$judul=$_POST['judul'];
            $idk=$_POST['id_kategori'];
            $isi=$_POST['isi'];
            $aktif=$_POST['aktif'];
            $sql=mysql_query("insert into artikel (judul,id_kategori,isi,aktif) values ('$judul','$idk','$isi','$aktif')");
if (sql) {
                        echo"<p align='center'>Berhasil<a href='home.php?page=artikel'>Kembali</a></p>";
            }
            else
            {
                        echo"<p align='center'>Gagal<a href='home.php?page=addartikel'>Ulangi</a></p>";
            }
            break;                                                              /// break tambah artikel
case "hapusartikel";
case "artikel": //===================artikel
                        $sql=mysql_query("select * from artikel join kategori using (id_kategori) where artikel.id_kategori=kategori.id_kategori");
            echo"
            <h3 align='center'>Data Artikel</h3>
            <table align='center' width='50%' border='1'>
            <tr>
                        <th>Judul</th>
                        <th>Kategori</th>
                        <th>Aktif</th>
                        <th colspan='2'>Aksi</th>
            </tr>";
                        while($data=mysql_fetch_array($sql))
                        {
                                    echo"
                                    <tr>
                                    <td align='center'>$data[judul]</td>
                                    <td align='center'>$data[kategori]</td>
                                    <td align='center'>$data[aktif]</td>
                                    <td align='center'><a href='home.php?page=editartikel&id=$data[id_artikel]'>Ubah</a></td>
                                    <td align='center'><a href='home.php?page=hapusartikel&id=$data[id_artikel]'>Hapus</a></td>
                                    </tr>";
            }
                        echo "</table>";
            break; //=================break artikel
$id=$_GET['id'];
$h=mysql_query("delete from artikel where id_artikel='$id'");
if ($h)
{
            echo "<p align='center'>Data Berhasil di Hapus</p>";
            echo "<p align='center'><a href='home.php?page=artikel'>Kembali</a></p>";
}
else
{
            echo "<p align='center'>Data Gagal di Hapus</p>";
            echo "<p align='center'><a href='home.php?page=artikel'>Kembali</a></p>";
}
break;










case "bukutamu": //===================buku tamu
                        $sql=mysql_query("select * from tamu");
echo"
<p align='center'> Buku Tamu </p>
                        <form action='home.php?page=tambahtamu' method='post'>
                                     <input type ='text' name='nama' placeholder='nama'> masukan nama anda* <br>
                                     <textarea name='pesan' placeholder='pesan' cols='65' row='8' tabindex='5' style='height:300px';></textarea><br>
                                    <input type ='reset' name='reset' value='Reset'>
                                                <input type ='submit' name='submit' value='simpan'>
                        </form>";
                        break;             
case "tambahtamu":
            $id=$_POST['id'];
            $nama=$_POST['nama'];
            $pesan=$_POST['pesan'];
if ($pesan=='' )
{
            echo"<p align='center'> Tidak Boleh Null</p>";
            echo"<p align='center'><a href='home.php?page=bukutamu'>Kembali</a></p>";
}                     
else
{         
            $sql=mysql_query("insert into tamu (id,nama,pesan) values ('$id','$nama','$pesan')");
            if (sql) {
                        echo"<p align='center'>Berhasil<a href='home.php?page=bukutamu'>Kembali</a></p>";
            }
            else
            {
                        echo"<p align='center'>Gagal<a href='home.php?page=tambahtamu'>Ulangi</a></p>";
            }
}         
            break;
}
?>
5.      home.php ‘halaman home’


<?php
session_start();
if ($_SESSION['id']=="")
{
            header('location:index.php?init=3');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
            <title>untitled</title>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <meta name="generator" content="Bluefish 2.2.3" />
            <link rel="stylesheet" href="admin.css" type="text/css"/>
            <link rel="stylesheet" href="ckeditor/content.css" type="text/css"/>
            <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
            <div id='header'>
            <img src='dani.jpg' width='900px'>
            </div>
            <div id='menu'>
            <ul>
                        <li><a href='home.php'>| Home |</a></li>
                        <li><a href='home.php?page=user'> User |</a></li>
                        <li><a href='home.php?page=kategori'> Kategori |</a></li>
                        <li><a href='home.php?page=addkategori'> Tambah Kategori |</a></li>
                        <li><a href='home.php?page=artikel'> Artikel |</a></li>
                        <li><a href='home.php?page=addartikel'> Tambah Artikel |</a></li>
                        <li><a href='home.php?page=bukutamu'> Buku Tamu |</a></li>
                        <li><a href='logout.php'> Logout |</a></li>
            </ul>
            </div>
            <frameset rows="2" cols="30%,70%">
            <frame  noresize="noresize" scrolling="">      
            <div id='isi'>
                        <?php
                                                include 'page.php';                  
                        ?>
                        <p>selamat datang di program CRUD</p>
                        </div>
            </frame>
            </frameset>
            <noframes></noframes>
</body>
</html>

6.      logout.php

<?php
session_start();
session_destroy();
header('location:index.php?init=2');

?>

KETERANGAN  : ketika berhasil logout akan kembali ke halamn login

No comments:

Post a Comment