banner_manager.php

include ("includes/koneksi.php");
	
	
//$_REQUEST['simpan'] = isset($_POST['simpan']) ? $_POST['simpan'] : '';
$link = isset($_POST['link']) ? $_POST['link'] : '';
$gambar = isset($_POST['gambar']) ? $_POST['gambar'] : '';
$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>

Above are banner_manager.php script. When I try to upload data:

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

The file Air Freshener POWER SPRAY.jpg has been uploaded

Link [_______________] ex. www.webaddress.com
Gambar [_______________] [Browse]
[Simpan]

Banner Action


The picture has been uploaded but I do not see it shown in Banner. When I checked phpmyadmin in banner table also still empty.

line 64: $gambar = $_REQUEST;

Recommended Answers

All 44 Replies

first off, don't use $_REQUEST, you should have $_POST or $_GET values, update your code to reflect that.
also to make sure $data isn't empty you may want to try something like:

if ($data = mysql_fetch_array($result)) {
    $id = $data['id'];
    $link = $data['link'];
    $gambar = $data['gambar'];
}

Well, the $id, $link and $gambar suppose to be passed from whatever I data I filled in the form and whenever I press save button (simpan button).

can I just replace $_REQUEST with $_GET (line 11 - 15)?

if (isset($_GET['simpan'])){
     $id = $_GET['id'];
     $link = mysql_real_escape_string($_GET['link']);
     $gambar = $_GET['gambar'];
     }

in which line should I enter the above code? It seems work. It just does not save the information that I enter in the form.

You'll want to replace it with $_POST... like the top of your form, also I don't think you can upload files with $_GET therefore your form should have a method=post on it.


line 60 is where the if 'if ($data = mysql_fetch_array($result)) {' statement should go.

Okay, I change it to $_POST

//SAVE DATA
	if (isset($_POST['simpan'])){
		$id = $_POST['id'];
		$link = mysql_real_escape_string($_POST['link']);
		$gambar = $_POST['gambar'];
		}

Now, it appears to work, yet when I check or refresh the data is not yet saved. I cannot see the picture nor the link saved.

------

Occassionally, I see:

Undefined index: gambar in C:\xampp\htdocs\Masterlink\cgoods\banner_manager.php on line 64

Line 64 is:

$gambar = $_POST['gambar'];

couldyou add print_r($_POST); die(); and comment out $gambar = $_POST;

This appears:

Array ( [link] => www.banner.com [id] => [simpan] => Simpan )

This appears:

Array ( [link] => www.banner.com [id] => [simpan] => Simpan )

there is no element with name gambar as you can see from array. Either!
Check your HTML form

This is the same stuff what i was searching for! Thanks for this informative stuff.

I have it in the html form.

<tr>
	<td>Gambar</td>
	<td><input type="file" name="uploaded_file"/></td>
</tr>

Yes but name is uploaded_file not gambar!
change

<td><input type="file" name="uploaded_file"/></td>

to

<td><input type="file" name="gambar"/></td>

Array ( [link] => www.banner.com [id] => [simpan] => Simpan )

Still the same.

Sorry for misleading. I was not careful enough to tell you that file tags goes to $_FILES array not $_POST!
So catch it there not in $_POST

you mean like this:

$gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';

you mean like this:

$gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';

Yes, have you tried that?

Yes. This array appears:

Array ( [link] => www.banner.com [id] => [simpan] => Simpan )

and the data inputed are not saved yet.

Yes. This array appears:

Array ( [link] => www.banner.com [id] => [simpan] => Simpan )

and the data inputed are not saved yet.

can you post your current code? I cannot understand what you mean by "not saved"

banner_manager.php

//$_REQUEST['simpan'] = isset($_POST['simpan']) ? $_POST['simpan'] : '';
   $link = isset($_POST['link']) ? $_POST['link'] : '';
   $gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';
   $id = isset($_POST['id']) ? $_POST['id'] : '';

	//SIMPAN DATA
	if (isset($_POST['simpan'])){
		$id = $_POST['id'];
		$link = mysql_real_escape_string($_POST['link']);
		//$gambar = $_POST['gambar'];
		
		print_r($_POST); die(); 
		
		echo $link;
		echo $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());
			
			if ($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="gambar"/></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>

I mean it is not inserted in the database yet as it suppose to. The link address suppose to be listed under banner if it is saved in the database.

what error does it give? Is can you echo your SQL string instead of doing mysql_query

INSERT INTO banner(link, gambar) VALUES('','')

something is wrong with your form as it is presenting empty variable to query. My Advice is make very tiny form and connect it to DB and make sure that it is working and transfer that to your code. I cannot help with this big chunk of code and limited time at hand. So minify your program to form and database and let us start from there

banner_managerTest.php

<?php
	
include ("includes/koneksi.php");
	
	
$link = isset($_POST['link']) ? $_POST['link'] : '';
$gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';

//SAVE DATA
if (isset($_POST['simpan'])){
	$id = $_POST['id'];
	$link = mysql_real_escape_string($_POST['link']);
	//$gambar = $_POST['gambar'];
		
	print_r($_POST); die(); 
		
	echo $link;
	echo $gambar;
	}
		
	//Cek the existance of uploaded file
	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."')";
	echo $sqlstr;
	}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 = "";
}
		
	
?>

<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; ?>"/> Example: http://www.banner.com
	<input type="hidden" name="id" value="<?php echo $id; ?>"/>
	</td>
	</tr>
	<tr>
	<td>Gambar</td>
	<td><input type="file" name="gambar"/></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>

I am reducing my original code to the above code. The layout basically like this:

Banner Manager

Link [___________] example: http://www.banner.com
Gambar [___________] [Browse]


and I am still receiving this output :

INSERT INTO banner(link, gambar) VALUES('','')

the table called banner which has 3 fields: id, link, and gambar (translate: picture)

I wonder why I press "simpan" (translate: save) button I do not see the data that I entered being saved ?

$gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';

If gambar is not set then it sets it to empty. this is bad. if file is empty you should redirect to the form as something is wrong with it!

$gambar = isset($_FILES['gambar']) ? $_FILES['gambar'] : '';

If gambar is not set then it sets it to empty. this is bad. if file is empty you should redirect to the form as something is wrong with it!

Do you mean saving empty form ? and I should give error mark that the user suppose to fill the empty form before saving it ?

That's a good point. but even when I fill in the form, I have not see the form capturing the data and insert them to the database. Because after I press save button and check the database none of the data is there.

but even when I fill in the form, I have not see the form capturing the data and insert them to the database. Because after I press save button and check the database none of the data is there.

If something goes wrong with isset($_FILES) then it will default to empty string. You can do var_dump($_FILES); to see if everything you are sending is actually available. from there you can know what to check!

If something goes wrong with isset($_FILES) then it will default to empty string. You can do var_dump($_FILES); to see if everything you are sending is actually available. from there you can know what to check!

This is the message:

array(1) { ["gambar"]=> array(5) { ["name"]=> string(29) "Air Freshener POWER SPRAY.jpg" ["type"]=> string(11) "image/pjpeg" ["tmp_name"]=> string(22) "C:\xampp\tmp\php92.tmp" ["error"]=> int(0) ["size"]=> int(281900) } } Array ( [link] => banner.com [id] => [simpan] => Simpan )

What does it mean ?

could you attach the files so that I test in my system?

ok, the file attached.

ok, the file attached.

Tracing down I believe here is a problem:

if (empty($id)){
    $sqlstr = "INSERT INTO banner(link, gambar) VALUES('".$link."','".$gambar."')";
    echo $sqlstr;

}else{

    $sqlstr = "UPDATE banner SET link = '".$link."',gambar = '".$gambar."' WHERE id =".$id;
    //other codes goes on....
}

But checking thru I found $gambar is an array containing:
'name' => string '956.png' (length=7)
'type' => string 'image/png' (length=9)
'tmp_name' => string '/tmp/phpvHQioh' (length=14)
'error' => int 0
'size' => int 627511
So Instead of using $gambar use either $gambar or whatever variable you want.
Sorry for being late, I have other things to do :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.