0

banner_manager.php

<?php
	//require("include/header.php");

	//if (!$_SESSION['login']){
	//	echo "Anda tidak berhak mengakses halaman ini.";
	//	exit();
	//}
	
	include ("includes/koneksi.php");
	
	
   //$_REQUEST['simpan'] = isset($_POST['simpan']) ? $_POST['simpan'] : '';
   //$_REQUEST['link'] = isset($_POST['link']) ? $_POST['link'] : '';
   //$_REQUEST['gambar'] = isset($_POST['gambar']) ? $_POST['gambar'] : '';
   //$_REQUEST['id'] = isset($_POST['id']) ? $_POST['id'] : '';

	//SIMPAN DATA
	if (isset($_REQUEST['simpan'])){
		$id = $_REQUEST['id'];
		$link = mysql_real_escape_string($_REQUEST['link']);
		$gambar = $_REQUEST['gambar'];
		}
		
		//Cek apakah ada file yang diupload
		if((!empty($_FILES['uploaded_file'])) && ($_FILES['uploaded_file']['error'] == 0)){
			//$gambar = uploadPicture('uploaded_file');
		
			$target_path = "Images/";

			$target_path = $target_path . basename( $_FILES['uploaded_file']['name']); 

			$gambar = $target_path; 
			
			if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
    			echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
   			" has been uploaded";
			
			// I add this to test insert data
			//mysql_query("INSERT INTO banner(link, gambar) VALUES('".$link."','".$gambar."')");
			
			} else{
 		   echo "There was an error uploading the file, please try again!";
			}

		}
		
		if (empty($id)){
			$sqlstr = "INSERT INTO banner(link, gambar) VALUES('".$link."','".$gambar."')";
		}else{
			$sqlstr = "UPDATE banner SET link = '".$link."',gambar = '".$gambar."' WHERE id =".$id;
		
		$result = mysql_query($sqlstr) or die(mysql_error());
				
		$confirmation = ($result)? "Data telah tersimpan.":"Gagal menyimpan data.";
		$gambar = "";
		$link = "";
		$id = "";
		}
		
	
	//EDIT / DELETE MODE
	if (!empty($_REQUEST['id']) && !empty($_REQUEST['mode'])){
		if ($_REQUEST['mode'] == 'delete'){
			$result = mysql_query("DELETE FROM banner WHERE id=".$_REQUEST['id']) or die(mysql_error());
			$confirmation = ($result)? "Data telah terhapus.":"Gagal menghapus data.";
		}elseif ($_REQUEST['mode'] == 'edit'){
			$result = mysql_query("SELECT * FROM banner WHERE id=".$_REQUEST['id']) or die(mysql_error());
			$data = mysql_fetch_array($result);
			$id = $data['id'];
			$link = $data['link'];
			$gambar = $data['gambar'];
		}
	}
	?>

<div align="center">
	<div style="width:700px;text-align:left;padding-top:25px;">
	<div class="pageTitle">Banner Manager</div>
	<?php //echo $confirmation; ?><br/>
	<form method="post" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']?>">
		<table width="700px" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td>Link</td>
			<td>
				<input type="text" name="link" value="<?php echo $link; ?>"/> Contoh: http://www.garudanews.co.cc
				<input type="hidden" name="id" value="<?php echo $id; ?>"/>
			</td>
		</tr>
		<tr>
			<td>Gambar</td>
			<td><input type="file" name="uploaded_file"/></td>
		</tr>
		<tr>
			<td colspan="2">
				<?php
				if (!empty($_REQUEST['id'])){
				?>
					<img src="<?php echo $gambar;?>" alt="gambar"/>
					<input type="hidden" name="gambar" value="<?php echo $gambar; ?>"/>
				<?php
				}
				?>
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" name="simpan" value="Simpan"/></td>
		</tr>
	</table>
	</form>
	<hr/>
	
	<table width="400px" border="1" cellpadding="2" cellspacing="0">
		<tr>
			<th>Banner</th>
			<th>Action</th>
		</tr>
		<?php
		//LOAD BANNER AND ACTION MODE
		$result = mysql_query("SELECT * FROM banner");
		while ($data = mysql_fetch_array($result)){
		?>
			<tr>
				<td><?php echo $data['link'];?></td>
				<td>
					<a href="./banner_manager.php?id=<?php echo $data['id']; ?>&amp;mode=delete">Hapus</a> | 
					<a href="./banner_manager.php?id=<?php echo $data['id']; ?>&amp;mode=edit">Edit</a>
				</td>
			</tr>
		<?php
		}
		?>
	</table>
	</div>
</div>
<?php	
	include('includes/footer.php');
?>

Notice: Undefined index: gambar in C:\xampp\htdocs\php_template2\banner_manager.php on line 21
The file garuda_media.jpg has been uploadedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=
Notice: Undefined variable: id in C:\xampp\htdocs\php_templa' at line 1


Line 21: $gambar = $_REQUEST;

Line 1: <?php


I don't understand the last error. This error basically appear after I try to upload a new image and link.

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by davy_yg
0

I wonder whether to use:

a) $gambar = isset($_POST) ? $_POST : '';

or

b) $_REQUEST = isset($_POST) ? $_POST : '';

both of them post similar errors undefined.

Notice: Undefined index: gambar in C:\xampp\htdocs\php_template2\banner_manager.php on line 64

For instance I use a)

line 64: $gambar = $_REQUEST;

if I use b)

Undefined variable: gambar in C:\xampp\htdocs\php_template2\banner_manager.php on line 91

line 91: $sqlstr = "INSERT INTO banner(link, gambar) VALUES('".$link."','".$gambar."')";

How to fix it?

0

Always use option a).

Your line numbers don't match with what you paste. Make sure they do, to keep us from guessing which line you mean.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.