MEMBUAT CRUD USER (INSERT, UPDATE, DELETE)
Membuat Tabel User yang berisi
Field
|
Type Data
|
Deskripsi/Length/Value
|
Id
|
Integer (5)
|
Primary Key, Auto Increment
|
Username
|
Varchar (100)
| |
Password
|
Varchar(100)
| |
Level
|
ENUM
|
‘1’,’2’,’3’
|
Status
|
ENUM
|
‘Y’,’N’
|
----------->>Membuat koneksi.php
<?php
mysql_connect("localhost","root"," ");
mysql_select_db("km");
?>
---------->>Membuat tambah_user.php
<?php
include ("koneksi.php");
?>
<?php
if (isset($_POST['tambah'])) {
$user=$_POST['user'];
$pass=md5($_POST['pass']);
$level=$_POST['level'];
$status=$_POST['status'];
$sql=mysql_query("insert into login values (' ','$user','$pass','$level','$status')");
if ($sql)
{
echo '<script>alert("Data berhasil ditambahkan")</script>';
} else {
echo '<script>alert("Data gagal ditambahkan")</script>';
}
}
?>
<form method="post">
<table width="273" border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2"><b>Tambah Data User</b></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="user" size="30"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" size="30"></td>
</tr>
<tr>
<td>Level</td>
<td><select name="level">
<option value="0">-- Silahkan Pilih --</option>
<option value="1">Administrator</option>
<option value="2">Kasir</option>
<option value="3">Direktur</option>
</select></td>
</tr>
<tr>
<td>Status</td>
<td><select name="status">
<option value="0">Belum Aktif</option>
<option value="1">Aktif</option>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="tambah" value="TAMBAH"></td></tr>
</table>
</form>
----------->>Membuat tampil_user.php
<?php
include "koneksi.php";
?>
<style type="text/css">
<!--
.style1 {
color: #FFFFFF;
font-style: italic;
font-weight: bold;
}
.style4 {font-weight: bold; font-family: Arial, Helvetica, sans-serif; color: #FFFFFF;}
-->
</style>
<table width="675" border="0" cellspacing="1" cellpadding="1">
<tr>
<td colspan="6" bgcolor="#999999"><div align="center" class="style4">DATA USER</div></td>
</tr>
<tr>
<td width="28" bgcolor="#FF0000"><div align="center" class="style1">Id</div></td>
<td width="108" bgcolor="#FF0000"><div align="center" class="style1">Username</div></td>
<td width="102" bgcolor="#FF0000"><div align="center" class="style1">Password</div></td>
<td width="59" bgcolor="#FF0000"><div align="center" class="style1">Level</div></td>
<td width="68" bgcolor="#FF0000"><div align="center" class="style1">Status</div></td>
<td width="291" bgcolor="#FF0000"><div align="center" class="style1">Action</div></td>
</tr>
<?php
$sql=mysql_query("select * from login");
while ($row=mysql_fetch_array($sql,MYSQL_NUM)) {
?>
<tr>
<td><?php echo $row['0'] ?></td>
<td><?php echo $row['1'] ?></td>
<td><?php echo $row['2'] ?></td>
<td><div align="center"><?php echo $row['3'] ?></div></td>
<td><div align="center"><?php echo $row['4'] ?></div></td>
<td>
<a href="?page=tambah_user">Add</a> |
<a href="?page=hapus_user&&delete=<?php echo $row[0] ?>"
onclick="return confirm(apakah anda yakin mau dihapus ?)">Delete </a> |
<a href="?page=edit_user&&ubah=<?php echo $row[0] ?>">Update</a>
</td>
</tr>
<?php
}
?>
</table>
--------->>Membuat Edit_user.php
<?php include ("koneksi.php"); ?>
<?php
if (isset($_GET['ubah'])) {
$id=$_GET['ubah'];
$sql=mysql_query("select * from login where id='$id' ");
$data=mysql_fetch_assoc($sql);
echo'
<form method="post">
<table width="273" border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2"><b>Update Data User</b></td>
</tr>
<tr>
<td>ID</td>
<td>
<input type="text" value="'.$data['id'].'" disabled></td>
<input type="hidden" name="id" size="30" value="'.$data['id'].'">
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="user" size="30" value="'.$data['username'].'"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" size="30" value="'.$data['password'].'"></td>
</tr>
<tr>
<td>Level</td>
<td><select name="level">
<option value="0">-- Silahkan Pilih --</option>
<option value="1">Administrator</option>
<option value="2">Kasir</option>
<option value="3">Direktur</option>
</select></td>
</tr>
<tr>
<td>Status</td>
<td><select name="status">
<option value="0">Belum Aktif</option>
<option value="1">Aktif</option>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="update" value="Update"></td></tr>
</table>
</form> '; }
if (isset($_POST['update'])) {
$id=$_POST['id'];
$user=$_POST['user'];
$pass=md5($_POST['pass']);
$level=$_POST['level'];
$status=$_POST['status'];
$sql2=mysql_query("update login set username='$user', password='$pass', level='$level', status='$status' where id='$id'");
if ($sql2) {
echo '<script>alert("Data berhasil di Update");window.location.assign("?page=tampil_user");</script>';
} else {
echo '<script>alert("Data GAGAL di Update")</script>';
}
}
?>
---------->>Membuat hapus_user.php
<?php include ("koneksi.php") ?>
<?php
if (isset($_GET['delete'])){
$id=$_GET['delete'];
$sql=mysql_query("delete from login where id='$id'");
if ($sql)
{
echo '<script>alert("Berhasil Hapus Data"); window.location.assign("?page=tampil_user");</script>';
} else {
echo '<script>alert("Gagal Hapus Data")</script>';
}
}
?>
0 komentar:
Posting Komentar