- Pada langkah awal kita buat sebuah tabel terlebih dahulu, silahkan buat sebuah tabel dengan nama tabel_berita
Name | Type | |
id | int(11) | AUTO_INCREMENT, PRIMARY KEY |
nama | varchar(250) | |
judul | varchar(250) | |
isiberita | varchar(1500) | |
photo | varchar(350) |
- Kemudian kita buat script untuk input data berita ke dalam database. Untuk lebih mudah, kita akan buat file insert data dan cara menampilkanya ke dalam satu file saja. Kita buat file dengan nama guest-berita.php
<? php
$host = "localhost";
$user = "root";
$pass = ""; //password sesuaikan dengan password db punya anda
$d_base = ""; // nama db silahkan disesuaikan sendiri
mysql_connect($host, $user, $pass) or die ("koneksi masih gagal, coba cek lagi script nya...".mysql_error());
mysql_select_db($d_base) or die ("Database tdk ditemukan, mungkin belum dibuat db nya... :) ".mysql_error());
?>
<html>
<head>
<title>Form Berita - </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(image/background.jpg);
background-repeat:no-repeat;
background-position:top;
font-family:"Courier New", Courier, monospace;
font-weight:bold;
}
.style11 {color: #000099}
.style12 {color: #993300}
-->
</style>
</head>
<body>
<?php
if($_POST['doSimpan'] == 'Simpan')
{
if(empty($nama) || strlen($nama) < 1)
{
$err[] = "ERROR - Namanya diisi dulu....!!!";
}
// Cek Photo
$tipe_gambar = array('image/jpeg','image/bmp', 'image/x-png');
$gbr = $_FILES['gambar']['name'];
$ukuran = $_FILES['gambar']['size'];
$tipe = $_FILES['gambar']['type'];
$error = $_FILES['gambar']['erorr'];
if($gbr !=="" && $ukuran > 0 && $error == 0){
if(in_array(strtolower($tipe), $tipe_gambar)){
$move=move_uploaded_file($_FILES['gambar']['tmp_name'], 'image/'.$gbr);
}
if(empty($err)) {
$sql_insert = "INSERT into `tabel_berita`
(`nama`,`photo`,`judul`,`isiberita`
)
VALUES
('$nama','$gbr','$judul','$isiberita'
)
";
mysql_query($sql_insert) or die("Insertion Failed:" . mysql_error());
echo "<div align='center'><strong><font color=red size=5>Data berhasil disimpan</font></strong></div>";
}
}
}
?>
</tr>
<tr>
<table width="800" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
<td>
<form action="guest-berita.php" method="post" name="regForm" id="regForm" enctype="multipart/form-data" >
<tr></br>
<td>
<td align="right">
<tr>
<?php
if(!empty($err)) {
echo "<div class=\"msg\">";
foreach ($err as $e) {
echo "* $e <br>";
}
echo "</div>";
}
?>
<tr>
<td bgcolor=""><span class="style11">Nama / Outhor</span><span class="required"><font color="#CC0000">*</font></span><br>
<input name="nama" type="text" id="nama" size="25" class="required"></td>
</tr>
<tr>
<td bgcolor="">
<input name="gambar" type="file" id="gambar" size="25" value="<?php echo $gbr['photo'];?>" />
</td>
</tr>
<tr>
<td bgcolor=""><span class="style11">Judul Berita</span><span class="required"><font color="#CC0000">*</font></span><br>
<input name="judul" type="text" id="judul" size="30" class="required"></textarea></td>
</tr>
<tr>
<td bgcolor=""><span class="style11">Isi Berita</span><span class="required"><font color="#CC0000">*</font></span><br>
<textarea name="isiberita" cols="30" rows="2" id="isiberita" class="required"></textarea></td>
</tr>
<tr>
<td><input name="doSimpan" type="submit" id="doSimpan" value="Simpan"></td>
<td> </td>
</tr>
</form>
<td>
</table>
</div>
</div>
</div>
<?php
//script dibawah ini untuk menampilkan data yang telah diinput ke dalam database
//untuk penomoran data
$no=0;
$query=mysql_query("SELECT * FROM tabel_berita order by id desc");
//menampilkan data
while($row=mysql_fetch_array($query)){
$no++;
?>
<p><?php echo $no ;?> . <?php echo $row['judul'];?> . <i>Oleh : <font color=brown><?php echo $row['nama'];?></font></i></p>
<p style="text-align: justify;"><img style="margin-right: 10px; float: left;" src="image/<?php echo $row['photo'];?>" alt="gambar artikel" width="100" height="100" /><?php echo $row['isiberita'];?></p>
<?php } ?>
</body>
</html>
$host = "localhost";
$user = "root";
$pass = ""; //password sesuaikan dengan password db punya anda
$d_base = ""; // nama db silahkan disesuaikan sendiri
mysql_connect($host, $user, $pass) or die ("koneksi masih gagal, coba cek lagi script nya...".mysql_error());
mysql_select_db($d_base) or die ("Database tdk ditemukan, mungkin belum dibuat db nya... :) ".mysql_error());
?>
<html>
<head>
<title>Form Berita - </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(image/background.jpg);
background-repeat:no-repeat;
background-position:top;
font-family:"Courier New", Courier, monospace;
font-weight:bold;
}
.style11 {color: #000099}
.style12 {color: #993300}
-->
</style>
</head>
<body>
<?php
if($_POST['doSimpan'] == 'Simpan')
{
if(empty($nama) || strlen($nama) < 1)
{
$err[] = "ERROR - Namanya diisi dulu....!!!";
}
// Cek Photo
$tipe_gambar = array('image/jpeg','image/bmp', 'image/x-png');
$gbr = $_FILES['gambar']['name'];
$ukuran = $_FILES['gambar']['size'];
$tipe = $_FILES['gambar']['type'];
$error = $_FILES['gambar']['erorr'];
if($gbr !=="" && $ukuran > 0 && $error == 0){
if(in_array(strtolower($tipe), $tipe_gambar)){
$move=move_uploaded_file($_FILES['gambar']['tmp_name'], 'image/'.$gbr);
}
if(empty($err)) {
$sql_insert = "INSERT into `tabel_berita`
(`nama`,`photo`,`judul`,`isiberita`
)
VALUES
('$nama','$gbr','$judul','$isiberita'
)
";
mysql_query($sql_insert) or die("Insertion Failed:" . mysql_error());
echo "<div align='center'><strong><font color=red size=5>Data berhasil disimpan</font></strong></div>";
}
}
}
?>
</tr>
<tr>
<table width="800" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
<td>
<form action="guest-berita.php" method="post" name="regForm" id="regForm" enctype="multipart/form-data" >
<tr></br>
<td>
<td align="right">
<tr>
<?php
if(!empty($err)) {
echo "<div class=\"msg\">";
foreach ($err as $e) {
echo "* $e <br>";
}
echo "</div>";
}
?>
<tr>
<td bgcolor=""><span class="style11">Nama / Outhor</span><span class="required"><font color="#CC0000">*</font></span><br>
<input name="nama" type="text" id="nama" size="25" class="required"></td>
</tr>
<tr>
<td bgcolor="">
<input name="gambar" type="file" id="gambar" size="25" value="<?php echo $gbr['photo'];?>" />
</td>
</tr>
<tr>
<td bgcolor=""><span class="style11">Judul Berita</span><span class="required"><font color="#CC0000">*</font></span><br>
<input name="judul" type="text" id="judul" size="30" class="required"></textarea></td>
</tr>
<tr>
<td bgcolor=""><span class="style11">Isi Berita</span><span class="required"><font color="#CC0000">*</font></span><br>
<textarea name="isiberita" cols="30" rows="2" id="isiberita" class="required"></textarea></td>
</tr>
<tr>
<td><input name="doSimpan" type="submit" id="doSimpan" value="Simpan"></td>
<td> </td>
</tr>
</form>
<td>
</table>
</div>
</div>
</div>
<?php
//script dibawah ini untuk menampilkan data yang telah diinput ke dalam database
//untuk penomoran data
$no=0;
$query=mysql_query("SELECT * FROM tabel_berita order by id desc");
//menampilkan data
while($row=mysql_fetch_array($query)){
$no++;
?>
<p><?php echo $no ;?> . <?php echo $row['judul'];?> . <i>Oleh : <font color=brown><?php echo $row['nama'];?></font></i></p>
<p style="text-align: justify;"><img style="margin-right: 10px; float: left;" src="image/<?php echo $row['photo'];?>" alt="gambar artikel" width="100" height="100" /><?php echo $row['isiberita'];?></p>
<?php } ?>
</body>
</html>
- Setelah selesai membuat tabel, dan script, jangan lupa untuk membuat folder dengan nama image ,folder tersebut berfungsi untuk menerima dan menyimpan gambar yang telah diupload. Sekarang silahkan anda coba untuk menginputkan data, Semoga berhasil
sumber : arofat
0 comments:
Post a Comment
ANDA SOPAN KAMI SEGAN :)