Nothing Special   »   [go: up one dir, main page]

Pertemuan 13 Pemrograman Web 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

MANIPULASI DATABASE

(LANJUTAN)

Membuat Form Isian Artikel

<?
include "koneksi.php";
?>
<h1>Form Berita</h1>
<form name=article method=post action=article.php>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="18%">Judul</td>
<td width="2%">:</td>
<td width="80%"><input type="text" name="title" size="30"
class="masukan"></td>
</tr>
<tr>
<td>Penulis</td>
<td>:</td>
<td><input type="text" name="author" size="30" class="masukan"></td>
</tr>
<tr>
<td>Lead</td>
<td>:</td>
<td><textarea name="abstraksi" rows="4" cols="45"></textarea></td>
</tr>
<tr>
<td>Content</td>
<td>:</td>
<td><textarea name="content" rows="8" cols="45" ></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Add" class="tombol">
<input type="reset" name="batal" value="Reset" class="tombol"></td>
</tr>
</table>
</form>
Proses Penambahan Artikel

<?
include "koneksi.php";

$judul= $_POST['title'];
$penulis = $_POST['author'];
$lead = $_POST['abstraksi'];
$isi = $_POST['content'];
$time=date("d M Y, H:i");

$lead = str_replace("\r\n","<br>",$lead);
$content = str_replace("\r\n","<br>",$content);

$query = "INSERT INTO articles (judul,penulis,lead,content,waktu)


values('$judul','$penulis','$lead','$isi','$time')";
$result = mysql_query($query);

if($result){
echo "<h3 align=center>Proses penambahan artikel berhasil</h3>";
echo "<A href=\"tampil_articles.php\">List</A>";
}else{
echo "<h2 align=center>Proses penambahan artikel tidak berhasil</h2>";
}
?>

Membuat Tampilan Artikel

<?php
include "koneksi.php";
$perintah="SELECT * FROM articles ORDER BY articleID DESC";
$hasil=mysql_db_query($dbname,$perintah);
echo("
<h2>List Artikel</h2>
<br><UL>
");

while ($row=mysql_fetch_array($hasil))
{
echo("
<LI>$row[1] &nbsp;$row[2] &nbsp; $row[waktu] &nbsp;<a
href=\"edit_article.php?articleID=$row[0]\">Edit</a>
&nbsp;<a
href=\"delete.php?articleID=$row[0]\">Hapus</a></LI><br>");
}
echo("</table>");
echo "<br><a href=\"add_article.php\">Tambah</a>";
echo "<br><a href=\"adminpanel.php\">Admin Panel</a>";
?>

Membuat Edit artikel

<?
include "koneksi.php";
$articleID = $_GET['articleID'];
// Query satu record artikel dari tabel sesuai nilai $articleID
$perintah="SELECT * FROM articles WHERE articleID =\"$articleID\"";
$hasil=mysql_db_query($dbname,$perintah);
$row=mysql_fetch_array($hasil);
$time=date("d M Y, H:i");

?>
<h1>Form Berita</h1>
<form name=article method=post action=update_article.php>
<input type="hidden" name="ID" value="<? echo "$row[articleID]" ?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="18%">Judul</td>
<td width="2%">:</td>
<td width="80%"><input type="text" name="title" size="50" class="masukan"
value="<? echo "$row[judul]" ?>"></td>
</tr>
<tr>
<td>Penulis</td>
<td>:</td>
<td><input type="text" name="author" size="50" class="masukan"
value="<? echo "$row[penulis]" ?>"></td>
</tr>
<tr valign="top">
<td>Lead</td>
<td>:</td>
<td><textarea name="abstraksi" rows="4" cols="50">
<? echo "$row[lead]" ?></textarea></td>
</tr>
<tr valign="top">
<td>Content</td>
<td>:</td>
<td><textarea name="content" rows="4" cols="50">
<? echo "$row[content]" ?></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>
<input type="submit" name="masuk" value="Update" class="tombol">
<input type="reset" name="hapus" value="Cancel" class="tombol"></td>
</tr>
</table>
</form>

Membuat Proses Edit Artikel

<?
include "koneksi.php";

$time=date("d M Y, H:i");
//$lead = str_replace("\r\n","<br>",$lead);
//$content= str_replace("\r\n","<br>",$content);
$update="UPDATE articles SET judul='$title', penulis='$author',
lead='$abstraksi',
content='$content', waktu='$time' WHERE articleID ='$ID'";
$hasil=mysql_db_query($dbname,$update);

if ($hasil) {
echo "Artikel berhasil di update<br>";
echo "<a href=\"tampil_articles.php\">List</a>";
} else {
echo "Artikel gagal di update";
}
?>

Membuat Delete Artikel

<?php
include "koneksi.php";
$articleID = $_GET['articleID'];

$perintah="DELETE FROM articles WHERE articleID =\"$articleID\"";


$hasil= mysql_query ($perintah);

if ($hasil) {
echo "Artikel berhasil dihapus<br>";
echo "<a href=\"edit_articles.php\">Back</a>";

} else {
echo "Komentar gagal dihapus. Kemungkinan terjadi kegagalan koneksi
ke database MySQL.";
}
?>

You might also like