0

I am trying to save data (link & picture) in banner manager.


banner_manager.php

//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['name'];
	}
		
	//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['name'] = $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['name']."')";
		echo $sqlstr;
	}else{
	$sqlstr = "UPDATE banner SET link = '".$link."',gambar = '".$gambar['name']."' WHERE id =".$id;
		
	$result = mysql_query($sqlstr) or die(mysql_error());
				
	$confirmation = ($result)? "Data telah tersimpan.":"Gagal menyimpan data.";
	$gambar['name'] = "";
	$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['name'] = $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="Images/<?php echo $gambar['name'];?>" width="100" height="100" alt="gambar"/>
				<input type="hidden" name="gambar" value="<?php echo $gambar['name']; ?>"/>
			<?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>

Here is the print out of sqlstr:

INSERT INTO banner(link, gambar) VALUES('www.link.com','Array')

Why the picture does not print out as the name of the picture ?

When I check the database, the data is not there either.

3
Contributors
11
Replies
12
Views
5 Years
Discussion Span
Last Post by davy_yg
0

You are getting that as an array probably because the following 2 input elements have the same name in your form. ( viz, name="gambar" )

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

and

<input type="hidden" name="gambar" value="<?php echo $gambar['name']; ?>"/>
0

Deleting one of them does not make any difference. I try to comment out the one on the bottom. I still getting similar output.

INSERT INTO banner(link, gambar) VALUES('www.banner.com','Array')

0

Add this line at the top of the page and check what data is posted by the form.

echo "<pre>";
print_r($_POST);
echo "<pre>";
0

I suggest that you change the name of file upload field from "gambar" to "gambarFileUpload". Also make appropriate changes where ever necessary to make the file upload handling script work correctly. You can keep the <input type="hidden" name="gambar" value="xyz" /> as it is. Also check if correct data is being saved to this hidden field.

0

see whats happening on this line, must be returning an array

19. $target_path = $target_path . basename( $_FILES);

also your upload if statement has no else - if it fails which might be causing the first error

15. if((!empty($_FILES['uploaded_file'])) && ($_FILES['uploaded_file']['error'] == 0)){.....
if(...){
}else{
//say error
}
}//<--else should be here to catch upload error
0

see whats happening on this line, must be returning an array

19. $target_path = $target_path . basename( $_FILES);

also your upload if statement has no else - if it fails which might be causing the first error

15. if((!empty($_FILES['uploaded_file'])) && ($_FILES['uploaded_file']['error'] == 0)){.....
if(...){
}else{
//say error
}
}//<--else should be here to catch upload error

I think I already have else in my code on line 30:

else{ 	   echo "There was an error uploading the file, please try again!";

Edited by davy_yg: n/a

0

I suggest that you change the name of file upload field from "gambar" to "gambarFileUpload". Also make appropriate changes where ever necessary to make the file upload handling script work correctly. You can keep the <input type="hidden" name="gambar" value="xyz" /> as it is. Also check if correct data is being saved to this hidden field.

How does it effect other line if I change file upload field to gambarFileUpload?

<td>Gambar</td>
		<td><input type="file" name="gambarFileUpload"/></td>
	</tr>
	<tr>			<td colspan="2">
	<?php				if (!empty($_REQUEST['id'])){
	?>
	<img src="Images/<?php echo $gambar['name'];?>" width="100" height="100" alt="gambar"/>
    <input type="hidden" name="gambar" value="<?php echo $gambar['name']; ?>"/>		<?php
	}
	?>
0

I think I already have else in my code on line 30:

else{ 	   echo "There was an error uploading the file, please try again!";

yeah you have 1 else for the if statement inside the upload, you don't actually have one for the upload error, my upload function has error reporting at each stage so you can see whats causing the error if you dont catch it you'll be left with a blank screen and no info on whats wrong.

error_reporting(E_ALL);//php will output all errors
//change "file" to your name
if ($_FILES["file"]["error"] > 0){
	echo "no error\r\n";
	if(move_uploaded_file($_FILES["file"]["tmp_name"],$folder.'/'. $_FILES['file']['name'])){
		echo "moved uploaded file to: {$folder}/{$_FILES['file']['name']}";
	}else{
		echo "failed to move file: {$folder}/{$_FILES['file']['tmp_name']}";
	}
}else{
	echo "upload error - Return Code: '.$_FILES["file"]["error"]."\r\n";
}
0

How does it effect other line if I change file upload field to gambarFileUpload?

<td>Gambar</td>
		<td><input type="file" name="gambarFileUpload"/></td>
	</tr>
	<tr>			<td colspan="2">
	<?php				if (!empty($_REQUEST['id'])){
	?>
	<img src="Images/<?php echo $gambar['name'];?>" width="100" height="100" alt="gambar"/>
    <input type="hidden" name="gambar" value="<?php echo $gambar['name']; ?>"/>		<?php
	}
	?>

It could be a problem because it is sent as post data to the next page, if they have the same name one will overwrite the other. a file upload may be different though im not sure.

Infact your problem may just be you don't have the file name correct in the php, $_FILES it is currently uploaded_file and your name for the file upload is gambar so it should be $_FILES

0

yeah you have 1 else for the if statement inside the upload, you don't actually have one for the upload error, my upload function has error reporting at each stage so you can see whats causing the error if you dont catch it you'll be left with a blank screen and no info on whats wrong.

error_reporting(E_ALL);//php will output all errors
//change "file" to your name
if ($_FILES["file"]["error"] > 0){
	echo "no error\r\n";
	if(move_uploaded_file($_FILES["file"]["tmp_name"],$folder.'/'. $_FILES['file']['name'])){
		echo "moved uploaded file to: {$folder}/{$_FILES['file']['name']}";
	}else{
		echo "failed to move file: {$folder}/{$_FILES['file']['tmp_name']}";
	}
}else{
	echo "upload error - Return Code: '.$_FILES["file"]["error"]."\r\n";
}

Could you try directly modify my code and post the result? Maybe I can test it and see if it works.

Right now, when I try to upload file, nothing happens, the file was not uploaded without any error message. Previously, the link was written correctly while the file was written as [array].

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.