Jump to content

Recommended Posts

So, after hours of debugging, I've finally got (for the most part) my multiple upload script working.

 

My problem, however, is that instead of inserting each images separate info into the database, it enters the last images information for all the images. I'm sure this has something to do with arrays and variables, but I don't know enough about arrays to figure it out on my own and after countless hours of google searches, I still haven't come up with anything.

 

So... if anyone would like to help out, it would be greatly welcomed.

 

The code is as follows:

<?php include "header.php"; ?>

<?php
//Check to see if form has been submitted
if (isset($_POST['submit'])) {
	require "dbconnect.php";		
		$uploaddir = '/home/ej3584/public_html/userpics/';

		for ($i = 0; $i < count($_FILES['image']['tmp_name']); $i++) {
			if(!empty($_POST['keywords'])) {
				$k = $_POST['keywords'];
			} else {
				$k = $_FILES['image']['name'];
			}

			if(!empty($_POST['comments'])) {
				$c = $_POST['comments'];
			} else {
				$c = "";
			}

			if (isset($_FILES['image']['tmp_name'][$i])) {
				$tempname = $_FILES['image']['tmp_name'][$i];
				$uid = mysql_insert_id();
				$uploaddir = '/home/ej3584/public_html/userpics/';
				$name = $_FILES['image']['name'][$i];
				$filename = $uploaddir . $name;
					$n = '/userpics/' . $name;

					if($tempname != '') {
						if(move_uploaded_file($tempname, $filename)) {
							chmod($filename, 0644); 
							echo "The file has been uploaded";
							$query = "INSERT INTO icons (image, keywords, comments) VALUES ('$n', '$k', '$c')";
							$result = mysql_query($query) or die ("Could not upload" . $_FILES['image']['name'] . ": " . mysql_error());

						} else {
								echo "The file could not be moved";
//								$query = "DELETE FROM icons WHERE id = $uid";
//								$result = mysql_query($query);
							}
					}
				} else {
					echo "Your submission was not processed.";
				}
			}
	}

?>
<!-- Show the form to upload icons -->
<h2>Upload LJ Icons</h2>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
	<table border="0" cellspacing="0" cellpadding="5" align="center" width="100%">
		<?php
			for ($i = 0; $i < 5; $i++) {
				echo "<tr><td width='15%'>Image #" . ($i + 1) . ": </td><td><input type='file' name='image[]'</td></tr>";
				echo "<tr><td>Keywords:</td><td><input type='text' name='keywords' /></td></tr>";
				echo "<tr><td>Comments:</td><td><input type='text' name='comments' /></td></tr>";
			}
				echo "<input name='process' type='hidden' value='1'>";
				echo "<tr><td colspan='2'><div align='center'><input type='submit' name='submit' value='Add Icons' /></td>";
				echo "</tr>";
		?>
	</table>
</form>

<!-- Show existing icons -->
<h2>Your User Icons</h2>
<?php
	require "dbconnect.php";
		//Get icons
		$sql = "SELECT * FROM icons";
		$result = mysql_query($sql) or die("Could not retrieve icons: " . mysql_error() );
			$i = 0;
			echo "<table border='0' cellspacing='0' cellpadding='5' width='100%'><tr>";
				while ($row = mysql_fetch_array($result)) {
					if(($i % 2) == 0 && ($i !=0)) {
						echo "</tr><tr>";
					}
					echo "<td><div align='center'><a href='editicons.php?id=" . $row['id'] . "'><img src=" . $row['image'] . " /></a><br />";
					echo $row['keywords'] . "<br />";
					echo $row['comments'] . "<br /></td>";
					$i++;
				}
			echo "</tr></table>";
?>	


<?php include "footer.php"; ?>

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/65282-multi-upload-script/
Share on other sites

The comments and keywords variables was onl;y looking at the last post for every record because the text boxs was not set as arrays.

 

<?php include "header.php"; ?>

<?php
//Check to see if form has been submitted
if (isset($_POST['submit'])) {
	require "dbconnect.php";		
		$uploaddir = '/home/ej3584/public_html/userpics/';
		for ($i = 0; $i < count($_FILES['image']['tmp_name']); $i++) {
			if(!empty($_POST['keywords'][$i])) {
				$k = $_POST['keywords'][$i];
			} else {
				$k = $_FILES['image']['name'][$i];
			}
			if(!empty($_POST['comments'][$i])) {
				$c = $_POST['comments'][$i];
			} else {
				$c = "";
			}
			if (isset($_FILES['image']['tmp_name'][$i])) {
				$tempname = $_FILES['image']['tmp_name'][$i];
				$uid = mysql_insert_id();
				$uploaddir = '/home/ej3584/public_html/userpics/';
				$name = $_FILES['image']['name'][$i];
				$filename = $uploaddir . $name;
					$n = '/userpics/' . $name;
					if($tempname != '') {
						if(move_uploaded_file($tempname, $filename)) {
							chmod($filename, 0644); 
							echo "The file has been uploaded";
							$query = "INSERT INTO icons (image, keywords, comments) VALUES ('$n', '$k', '$c')";
							$result = mysql_query($query) or die ("Could not upload" . $_FILES['image']['name'] . ": " . mysql_error());
						} else {
								echo "The file could not be moved";
//								$query = "DELETE FROM icons WHERE id = $uid";
//								$result = mysql_query($query);
							}
					}
				} else {
					echo "Your submission was not processed.";
				}
			}
	}
?>
<!-- Show the form to upload icons -->
<h2>Upload LJ Icons</h2>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
	<table border="0" cellspacing="0" cellpadding="5" align="center" width="100%">
		<?php
			for ($i = 0; $i < 5; $i++) {
				echo "<tr><td width='15%'>Image #" . ($i + 1) . ": </td><td><input type='file' name='image[]'</td></tr>";
				echo "<tr><td>Keywords:</td><td><input type='text' name='keywords[]' /></td></tr>";
				echo "<tr><td>Comments:</td><td><input type='text' name='comments[]' /></td></tr>";
			}
				echo "<input name='process' type='hidden' value='1'>";
				echo "<tr><td colspan='2'><div align='center'><input type='submit' name='submit' value='Add Icons' /></td>";
				echo "</tr>";
		?>
	</table>
</form>
<!-- Show existing icons -->
<h2>Your User Icons</h2>
<?php
	require "dbconnect.php";
		//Get icons
		$sql = "SELECT * FROM icons";
		$result = mysql_query($sql) or die("Could not retrieve icons: " . mysql_error() );
			$i = 0;
			echo "<table border='0' cellspacing='0' cellpadding='5' width='100%'><tr>";
				while ($row = mysql_fetch_array($result)) {
					if(($i % 2) == 0 && ($i !=0)) {
						echo "</tr><tr>";
					}
					echo "<td><div align='center'><a href='editicons.php?id=" . $row['id'] . "'><img src=" . $row['image'] . " /></a><br />";
					echo $row['keywords'] . "<br />";
					echo $row['comments'] . "<br /></td>";
					$i++;
				}
			echo "</tr></table>";
?>	
<?php include "footer.php"; ?>

Link to comment
https://forums.phpfreaks.com/topic/65282-multi-upload-script/#findComment-326032
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.