crud php dasar

delete.php
frm-inser.php
frm-update.php
inc-db.php
index.php
proses-insert.php
proses-update.php



delete.php
<?php
require_once('inc-db.php');
$sql="delete from tb_hobi where hobi_id='".$_GET['id']."'";
$result=mysql_query($sql);
if($result){
header('location: index.php');
}else{
echo "Gagal";
}

?>

frm-insert.php
<html>
<head>
<title>Update data Hobi</title>
</head>
<body>
<form method="post" action="proses-insert.php">
Hobi <input type="text" name="frm_hobi"><br>
<input type="submit" name="update" value="Update">
</form>
</body>
</html>

frm-update.php
<html>
<head>
<title>Update data Hobi</title>
</head>
<body>
<?php
require_once('inc-db.php');
$sql="select * from tb_hobi where hobi_id='".$_GET['id']."'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
?>
<form method="post" action="proses-update.php">
<input type="hidden" name="id" value="<?php echo $data['hobi_id']; ?>">
Hobi <input type="text" name="frm_hobi" value="<?php echo $data['hobi_nama']; ?>"><br>
<input type="submit" name="update" value="Update">
</form>
</body>
</html>

inc-db.php
<?php
mysql_connect('localhost','root','');
mysql_select_db('db_test');
?>

index.php
<html>
<head>
<title>Hallo World</title>
</head>
<body>
<h2>Data Hobi</h2>
<a href="frm-insert.php">Tambah Hobi</a>
<table>
<tr><td>No</td><td>Nama Hobi</td><td>Action</td></tr>
<?php
require_once('inc-db.php');
$sql="select * from tb_hobi order by hobi_id desc";
$result=mysql_query($sql);
$no=0;
while($data=mysql_fetch_array($result)){
$no++;
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $data['hobi_nama']; ?></td>
<td><a href="frm-update.php?id=<?php echo $data['hobi_id']; ?>">Edit |
<a href="delete.php?id=<?php echo $data['hobi_id']; ?>">Delete</a>
</tr>

<?php
}
?>
</table>
</body>
</html>

proses-insert.php
<?php
require_once('inc-db.php');
$var_hobi=$_POST['frm_hobi'];
$sql="insert into tb_hobi (hobi_nama) values ('$var_hobi')";
$result=mysql_query($sql);
if($result){
header('location: index.php');
}else{
echo "gagal";
}
?>

proses-update.php
<?php
require_once('inc-db.php');
$var_hobi=$_POST['frm_hobi'];
//echo $var_hobi; exit;

$sql="update tb_hobi set hobi_nama='$var_hobi' where hobi_id='".$_POST['id']."'";
$result=mysql_query($sql);
if($result){
header('location: index.php');
}else{
echo "gagal";
}
?>

0 Response to "crud php dasar"

Posting Komentar