Advertisement
cdsatrian

simpan-edit-delete berita

Aug 2nd, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2. include "../include/session.php";
  3. if(isset($_POST['SubmitH'])){
  4.   $aksi =isset($_GET['act'])?$_GET['act']:"";
  5.   $id =isset($_GET['id'])?$_GET['id']:"";
  6.   $TxtKodeH =(isset($_POST['TxtKodeH']))?$_POST['TxtKodeH']:"";
  7.   $TxtJudul = $_POST['TxtJudul'];
  8.   $TxtBerita = htmlentities((string)$_POST['TxtBerita']);
  9.   $nama_file = $_FILES['file']['name'];
  10.   $lokasi_file = $_FILES['file']['tmp_name'];
  11.   $tipe_file = $_FILES['file']['type'];
  12.   $size_file = $_FILES['file']['size'];
  13.   $direktori = "../image/image_berita/$nama_file";
  14.   $nama_file=!empty($lokasi_file)?$nama_file:'invalid.jpg';
  15.   function cekDimensiMax($lokasi_file){
  16.     $lebar_max = 1600;
  17.     $tinggi_max = 1350;
  18.     $hasil = 0;
  19.     if(!empty($lokasi_file)){
  20.       $ukuran_asli = getimagesize($lokasi_file);
  21.       if ($ukuran_asli[0] > $lebar_max and $ukuran_asli[1] > $tinggi_max ) {
  22.         $hasil = 1;
  23.       }
  24.     }
  25.     return $hasil;
  26.   }
  27.  
  28.   function cekInput($TxtJudul,$TxtBerita,$lokasi_file,$tipe_file,$size_file,$cekMax,$direktori){
  29.     $msg='';
  30.     if(empty($TxtJudul)){
  31.       $msg.="<b>--> Judul Berita Belum Diisi <--</b>";
  32.     }
  33.     elseif(empty($TxtBerita)){
  34.       $msg.="<b>--> Isi Berita Belum Diisi <--</b>";
  35.     }
  36.     else{
  37.       if(!empty($lokasi_file)){
  38.         if($tipe_file != "image/jpeg" and $tipe_file !="image/png"){
  39.           $msg.="<b>Tipe Gambar Harus *.jpg atau *.png</b>";
  40.         }
  41.         elseif($size_file > 1000000){
  42.           $msg.="<b>Max File 1 Mb</b>";
  43.         }
  44.         elseif($cekMax==1){
  45.           $msg.="<b>Max Dimensi 1600 x 1350 pixel</b>";
  46.         }else{
  47.           move_uploaded_file($lokasi_file,$direktori);
  48.         }
  49.       }  
  50.     }
  51.     return $msg;
  52.   }
  53.  
  54.   // Panggil fungsi dimensi dan definisikan
  55.   $cekMax = cekDimensiMax($lokasi_file);
  56.   # aksi simpan berita
  57.  if($aksi == "simpan"){
  58.     $msg=cekInput($TxtJudul,$TxtBerita,$lokasi_file,$tipe_file,$size_file,$cekMax,$direktori);
  59.     if(!empty($msg)){
  60.       echo $msg;
  61.       include "tambahBerita.php";
  62.     }else{
  63.       $sql = "INSERT INTO berita (judul,gambar,isi_berita,hari,tanggal,jam)
  64.      VALUES ('$TxtJudul','$nama_file','$TxtBerita','$hari_ini','$tgl_sekarang','$jam_sekarang')";
  65.       mysql_query($sql, $koneksi) or die("SQL Error !!".mysql_error());
  66.       echo "<script>alert('Berita Berhasil Disimpan');window.location.href='login.php?hal=daftar-berita';</script>";
  67.     }
  68.   }  
  69.   # aksi edit berita
  70.  if($aksi == "edit"){
  71.     $msg=cekInput($TxtJudul,$TxtBerita,$lokasi_file,$tipe_file,$size_file,$cekMax,$direktori);
  72.     if(!empty($msg)){
  73.       echo $msg;
  74.       include "editBerita.php";
  75.     }else{
  76.       $sql = "UPDATE berita SET judul='$TxtJudul',gambar='$nama_file',isi_berita='$TxtBerita',hari='$hari_ini',tanggal='$tgl_sekarang',jam='$jam_sekarang'
  77.      WHERE id_berita='$TxtKodeH'";
  78.       mysql_query($sql, $koneksi) or die("SQL Error !!".mysql_error());
  79.       echo "<script>alert('Berita Berhasil Diubah');window.location.href='login.php?hal=daftar-berita';</script>";
  80.     }
  81.   }
  82.   # aksi hapus berita
  83.  if($aksi == "hapus"){
  84.     $sql = "DELETE FROM berita WHERE id_berita='$id'";
  85.     mysql_query($sql, $koneksi) or die("SQL Error !!".mysql_error());
  86.     echo "<script>alert('Berita Berhasil Dihapus');window.location.href='login.php?hal=daftar-berita';</script>";
  87.   }
  88. }
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement