Edit dan hapus data (mysql php)

Tinggalkan komentar

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

MENAMPILKAN DATA (mysql & php)

2 Komentar

Berdasarkan pengurutan

ASC/DESC

Contoh :

Tanpa tabel :

<?php
include”koneksi.php”;
$result=mysql_query(“SELECT * FROM tamu order by id desc”);
$row = mysql_fetch_array($result);
?>
<?php
echo”Nama :$row[nama]<br>”;
echo”Email : $row[email]<br>”;
echo”No Telpon : $row[notelp]<br>”;
echo”Pesan : . $row[pesan]<br> .”;
?>

Dengan tabel :

<?php
include”koneksi.php”;
$result = mysql_query(“SELECT * FROM tamu ORDER BY id DESC”);
$row = mysql_fetch_array($result);
?>
<table width=”200″ border=”0″>
<tr>
<td>nama</td>
<td>:</td>
<td><?php echo “$row[nama]”;?></td>
</tr>
<tr>
<td>email</td>
<td>:</td>
<td><?php echo “$row[email]”; ?></td>
</tr>
<tr>
<td>no telpon</td>
<td>:</td>
<td><?php echo “$row[notelp]”; ?></td>
</tr>
<tr>
<td>pesan</td>
<td>:</td>
<td><?php echo “$row[pesan]”; ?></td>
</tr>
</table>

Secara langsung /tampil semua menurun 

Contoh :

Tanpa tabel :

<?php
include”koneksi.php”;
$result = mysql_query(“SELECT * FROM tamu”);

while($row = mysql_fetch_array($result))
{
echo $row[‘nama’] . ” ” . $row[’email’] . ” ” . $row[‘notelp’] . ” ” . $row[‘pesan’];
echo “<br />”;
}
?>

Dengan tabel :

<?php
include”koneksi.php”;
$result = mysql_query(“SELECT * FROM tamu”);
echo “<table border=’1′>
<tr>
<th>nama</th>
<th>email</th>
<th>telpon</th>
<th>pesan</th>
</tr>”;
while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘nama’] . “</td>”;
echo “<td>” . $row[’email’] . “</td>”;
echo “<td>” . $row[‘notelp’] . “</td>”;
echo “<td>” . $row[‘pesan’] . “</td>”;
echo “</tr>”;
}
echo “</table>”;
?>

 

 

tambah data (mysql dan php)

Tinggalkan komentar

script untuk menambah data koneksi php mysql

mysql_query ("insert into namatabel(field1,fiel2,field3)values('$field1','$field2','$field3');");

contoh :

<?php
$nama=$_POST[‘nama’];
$email=$_POST[’email’];
$notelp=$_POST[‘notelp’];
$pesan=$_POST[‘pesan’];
include”koneksi.php”;
$simpan=mysql_query(“insert into tamu(nama,email,notelp,pesan)values(‘$nama’,’$email’,’$notelp’,’$pesan’);”);
if($simpan){echo”data tersimpan”;}
else{echo”gagal disimpan”;}
?>