Langsung kita coba langah pertama tentunya kita buat dulu database mysqlnya dulu.
beri nama database bejalar
==============================================
`id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`nama` VARCHAR( 20 ) NOT NULL ,
`alamat` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;
==============================================
langkah selanjutnya kita buat form untuk menampilkan data nya
==============================================
<table width="97%" border="1" cellspacing="1" cellpadding="1">
<tr>
<th width="4%" scope="col">ID</th>
<th width="17%" scope="col">NAMA </th>
<th width="57%" scope="col">ALAMAT</th>
<th width="22%" scope="col">ACTION</th>
</tr>
<?php
$host="localhost";
$userdb="root";
$passdb="";
$dbo="bejalar";
$sambung=mysql_pconnect($host,$userdb,$passdb);
mysql_select_db($dbo,$sambung);
$query=mysql_query("SELECT data.id,data.nama,data.alamat
FROM data ");
while ($row = mysql_fetch_array ($query)) {
$id=$row['id'];
$nama=$row['nama'];
$alamat=$row['alamat'];
?>
<tr>
<td><?=$id;?></td>
<td><?=$nama;?></td>
<td><?=$alamat;?></td>
<td><a href="update.php?edit&id<?=$id;?>">Update</a></td>
</tr>
<? } ?>
</table>
==============================================
Simpan dengan nama tampil.php
Kemudian kita bikin form untuk updatenya
==============================================
<?php
$host="localhost";
$userdb="root";
$passdb="";
$dbo="bejalar";
$sambung=mysql_pconnect($host,$userdb,$passdb);
mysql_select_db($dbo,$sambung);
$id= $_GET['id'];
$query = " SELECT * FROM data WHERE id = $id";
$sql = mysql_query ($query);
$row = mysql_fetch_array ($sql);
?>
<FORM ACTION="proses_update.php" METHOD="POST" >
<table width="97%" border="1" cellspacing="1" cellpadding="1">
<tr>
<th width="8%" scope="col">ID</th>
<th width="28%" scope="col">NAMA </th>
<th width="64%" scope="col">ALAMAT</th>
<th width="28%" scope="col">EDIT</th>
</tr>
<tr>
<td><input type="text" name="id" disabled="disabled" value="<?=$row['id'];?>" /></td>
<td><input type="text" name="nama" value="<?=$row['nama'];?>" /></td>
<td><textarea name="alamat" cols="60" rows="2"><?=$row['alamat'];?>
</textarea></td>
<td>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<input type="submit" name="submit" value="SAVE" /></td>
</tr>
</table></FORM>
==============================================
Simpan dengan file update.php
yang terakhir kita bikin untuk memproses data update nya
==============================================
<?php
$host="localhost";
$userdb="root";
$passdb="";
$dbo="bejalar";
$sambung=mysql_pconnect($host,$userdb,$passdb);
mysql_select_db($dbo,$sambung);
$id= $_POST['id'];
$nama= $_POST['nama'];
$alamat= $_POST['alamat'];
$query = "UPDATE data SET nama='$nama',alamat='$alamat'
WHERE id='$id'" ;
$hasil = mysql_query($query);
//see the result
if ($hasil) {
echo "<p align='center'> update data success </p>";
include "tampil.php";
}else{
echo "Data gagal di update";
}
?>
==============================================
Simpan dengan nama proses_update.php
Tutorial cara membuat form update dengan php
semoga bermanfaat.
No comments:
Post a Comment