Sebelumnya kita tampilkan dulu data yg maw di update

contoh :

<?php
include”koneksi.php”;
$result = mysql_query(“SELECT * FROM tamu”);
echo “<table border=’1′>
<tr>
<th>No</th>
<th>nama</th>
<th>email</th>
<th>telpon</th>
<th>pesan</th>
<th colspan=’2′> Aksi</th>
</tr>”;
$i=1;
while($row = mysql_fetch_array($result))
{
echo “<tr>
<td> $i </td>
<td>$row[nama]</td>
<td>$row[email]</td>
<td>$row[notelp]</td>
<td>$row[pesan]</td>
<td><a href=edit.php?id=$row[id]>Edit</a></td>
<td><a href=hapus.php?id=$row[id]>Hapus</a></td>
</tr>”;
$i++;
}
echo “</table>”;
?>

Buat script untuk imput data baru

<?php
$id = $_GET[‘id’];
$row = mysql_query(“select * from tamu where id=’$id'”);
?>
<form method=”post” action=p_edit.php?id=<?php echo”$id”;?>>
<table border=”1″>
<tr>
<td>Nama</td><td>:</td><td><input type=”text” name=”nama”></td>
<tr></tr>
<td>Email</td><td>:</td><td><input type=”text” name=”email”></td>
<tr></tr>
<td>No Telpon</td><td>:</td><td><input type=”text” name=”notelp”></td>
<tr></tr>
<td>Pesan</td><td>:</td><td><textarea name=”pesan” cols=”1″ rows=”1″></textarea></td>
</tr>
</table>
<input type=”submit” value=”ubah”><input type=”reset” value=”hapus”>
</form>

Buat Script proses pengubahan data

<?php
include”koneksi.php”;
$id = $_GET[‘id’];
$nama=$_POST[‘nama’];
$email=$_POST[’email’];
$notelp=$_POST[‘notelp’];
$pesan=$_POST[‘pesan’];
$simpan=mysql_query(“update tamu SET nama=’$nama’,email=’$email’,notelp=’$notelp’,pesan=’$pesan’ where id=’$id'”);
if($simpan){echo”data berhasil diupdate “;}
else{echo”gagal diupdate”;}
?>

Script hapus data

<?php
include”koneksi.php”;
$id=$_GET[‘id’];
$hapus=mysql_query(“delete from tamu where id=’$id'”);
if($hapus)
{echo “data terhapus”;}
else
{echo “data gagal dihapus”;}
?>
<hr>
<a href=”lihat.php”>lihat data</a>
<br><a href=”guesbook.php”>input data</a>

Iklan