Selasa, 26 Mei 2015

cara membuat laman admin di php

pertama buat database di MySQL dengan nama "ujiadmin" dan buat tabel "admin" dengan field berikut: "id_admin, nama_admin, username, password, dan status_admin". untuk panjang karakter terserah teman-teman.
kemudian siapkan file-file berikut ini terlebih dahulu:

  • koneksi.php
  • tambah.php
  • hapus.php
  • view.php
  • update.php
koneksi.php
<?php
$server = 'localhost';
$username = 'root';
$password = "";
$database = 'ujiadmin';
mysql_connect($server, $username, $password)or die("Gagal Koneksi");
mysql_select_db($database)or die("Tidak ada database");
?>

tambah.php
<?php
include 'koneksi.php';
?><form method="post">
<p>
<label for="id_admin">ID Admin</label>
<input type="text"id="id_admin" name="id_admin"/>
</p>
<p>
<label for="nama_admin">Nama Admin</label>
<input type="text" id="nama_admin" name="nama_admin"/>
</p>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
</p>
<p>
<label>Status Admin</label>
<select name="status_admin">
<option>Aktif</option>
<option>Tidak Aktif</option>
</select>
</p>
<br/>
<p>
<label></label>
<input type="submit" value="Simpan" name="simpan"/>
<input type="reset" value="Batal"/>
</p>
</form>
<?php
if(isset($_POST['simpan'])){
$a=$_POST['id_admin'];
$b=$_POST['nama_admin'];
$c=$_POST['username'];
$d=$_POST['password'];
$e=$_POST['status_admin'];
$hasil=mysql_query("Insert into admin values('$a','$b','$c','$d','$e')");
if($hasil){
?>
<br/>
<span>Inserted Success</span>
<?php
}
}
?>
=============================
<br>
<?php
include 'koneksi.php';
?>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Username</th>
<th>Password</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php
$result = mysql_query("select*from admin order by id_admin desc");
while($data=mysql_fetch_array($result)){?>
<tr>
<td><?php echo $data['id_admin']?></td>
<td><?php echo $data['nama_admin']?></td>
<td><?php echo $data['username']?></td>
<td><?php echo $data['password']?></td>
<td><?php echo $data['status_admin']?></td>
<td><a href="update.php?u=<?php echo $data['id_admin']?>">edit</a> <br/> <a href="hapus.php?d=<?php echo $data['id_admin']?>" > hapus</a></td>
</tr>
<?php
}
?>
</table>

hapus.php
<?php
include 'koneksi.php';
$d = $_GET['d'];
$hasil = mysql_query("delete from admin where id_admin='$d'");
if($hasil){
header('location: view.php');
}
?>

view.php
<?php
include 'koneksi.php';
?>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Username</th>
<th>Password</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php
$result = mysql_query("select*from admin order by id_admin desc");
while($data=mysql_fetch_array($result)){?>
<tr>
<td><?php echo $data['id_admin']?></td>
<td><?php echo $data['nama_admin']?></td>
<td><?php echo $data['username']?></td>
<td><?php echo $data['password']?></td>
<td><?php echo $data['status_admin']?></td>
<td><a href="update.php?u=<?php echo $data['id_admin']?>">edit</a> <br/> <a href="hapus.php?d=<?php echo $data['id_admin']?>" > hapus</a></td>
</tr>
<?php
}
?>
</table>

update.php
<?php
include 'koneksi.php';
?>
<?php
$u =$_GET['u'];
$result = mysql_query("select * from admin where id_admin='$u'");
while ($data=  mysql_fetch_array($result)){
?>
<form method="post">
<p>
<input type="hidden" id="id_admin" name="id_admin" value="<?php echo $data['id_admin'] ?>"/>
</p>
<p>
<label for="nama_admin">Nama Admin</label>
<input type="text" id="nama_admin" name="nama_admin" value="<?php echo $data['nama_admin'] ?>"/>
</p>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username" value="<?php echo $data['username'] ?>"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password" value="<?php echo $data['password'] ?>"/>
</p>
<p>
<label>Status Admin</label>
<select name="status_admin">
<option>Aktif</option>
<option>Tidak Aktif</option>
</select>
</p>
<br/>
<p>
<label></label>
<input type="submit" value="Update" name="update"/>
</p>
</form>
<?php
}
if(isset($_POST['update'])){
$a=$_POST['id_admin'];
$b=$_POST['nama_admin'];
$c=$_POST['username'];
$d=$_POST['password'];
$e=$_POST['status_admin'];
$hasil = mysql_query("update admin set nama_admin='$b', username='$c', password='$d', status_admin='$e' where id_admin='$u'");
if($hasil){
?>
<br/>
<span>Updated Success</span>
<?php
echo '<script>window.location: view.php;</script>';
}
}
?>

6 komentar: