4 Replies - 8574 Views - Last Post: 24 January 2010 - 04:58 PM Rate Topic: -----

#1 johnd07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Uploading Images into MYSQL Database...

Post icon  Posted 19 January 2010 - 10:24 PM

Hey Guys, what's up? ... i'm having a problem when I try uploading images to my database...

My code uploads to first image but the remaining four images return an error which reads "unidentified index name2" and repeats the same error for name3.4&5...

I sought the help of some tutorials but most if not all only allowed for uploading one image where as I'd like to upload five images into the same row...

here's the code

<?php
//The following code is the PHP form handling application.
//Connecting to the MySQL database
require('dbconnect.php');

if (isset ($_POST['postarticle'])){
//Simplifying the variables.
$name = isset($_POST['name']) ? $_POST['name'] : NULL;
$type = isset($_POST['type']) ? $_POST['type'] : NULL;
$description = isset($_POST['description']) ? $_POST['description'] : NULL;
$content = isset($_POST['content']) ? $_POST['content'] : NULL;


$content = mysql_real_escape_string($content);
$description = mysql_real_escape_string($description);

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
		// This is an array that holds all the valid image MIME types
		$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

		if (in_array($file['type'], $valid_types))
				return 1;
		return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
		echo "<pre>";
		print_r($array);
		echo "</pre>";
}


// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$image = $_FILES['image'];
$image2 = $_FILES['image2'];
$image3 = $_FILES['image3'];
$image4 = $_FILES['image4'];
$image5 = $_FILES['image5'];



$image['name'] = mysql_real_escape_string($image['name']);
$image2['name2'] = mysql_real_escape_string($image['name2']);
$image3['name3'] = mysql_real_escape_string($image['name3']);
$image4['name4'] = mysql_real_escape_string($image['name4']);
$image5['name5'] = mysql_real_escape_string($image['name5']);


// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];
$TARGET_PATH .= $image['name2'];
$TARGET_PATH .= $image['name3'];
$TARGET_PATH .= $image['name4'];
$TARGET_PATH .= $image['name5'];



//Checks for empty fields or invalid date.
if(is_null($name) || is_null($content)  || is_null($type) || is_null($description)){
		echo "All fields must be fill!";
}else{

if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
//The MySQL query which will insert content into the table.
$query = "INSERT INTO  `eyecandy` (`eid` ,`name` ,`description` ,`content` ,`image1` ,`image2` ,`image3`,`image4`,`image5`,`type`) VALUES ('', '$name', '$description', '$content', '$image[name]','$image2[name2]','$image3[name3]','$image4[name4]','$image5[name5]','$type')";
//Executing the query with mysql_query().
mysql_query($query) or die(mysql_error());
echo "<center><strong>New Entry added!!!</strong></center>";
}
}
}


//Closing the connection.
mysql_close($conn);
?>

<br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table align="center">
<tr>
  <td align="right">Name:</td>
  <td><input type="text" name="name" maxlength="250" size="90"/></td></tr>
<tr>
  <td align="right">Short Description:</td>
  <td><input type="text" name="description" maxlength="140" size="90"/></td></tr>
<tr>
  <td align="right">Type:</td><td><select name="type" size="2">
  <option>Eyecandy</option>
<option>Studs</option>
</select></td>
</tr>
<tr>
  <td align="right">Content:</td>
  <td><textarea name="content" cols="50" rows="10"></textarea></td>
</tr>
<tr>
  <td align="right">Main Image:</td><td>
					<input type="file" name="image" />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</td></tr>
<tr>
  <td align="right">Image2:</td>
  <td>
					<input type="file" name="image2" />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</td></tr>
<tr>
  <td align="right">Image3:</td>
  <td>
					<input type="file" name="image3" />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</td></tr>
<tr>
  <td align="right">Image4:</td>
  <td>
					<input type="file" name="image4" />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</td></tr>
<tr>
  <td align="right">Image5:</td>
  <td>
					<input type="file" name="image5" />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</td></tr>


<tr><td> </td><td><input type="submit" name="postarticle" value="Enter New Entry" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form></div></td>
			</tr>
			<tr>
			  <td id="contentbottom"><div style="color:#FFFFFF; margin-left:10px;">iLevel EntertainmentiLevel EntertainmentiLevel EntertainmentiLevel EntertainmentiLevel Entertainment</div></td>
			</tr>
		  </table>


also I should state that I have no clue as to what to do :(...

any help will be useful!

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Uploading Images into MYSQL Database...

#2 johnd07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Uploading Images into MYSQL Database...

Posted 23 January 2010 - 06:09 PM

bump
Was This Post Helpful? 0
  • +
  • -

#3 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Re: Uploading Images into MYSQL Database...

Posted 23 January 2010 - 06:56 PM

First off why do you want to upload them into a database? Second have a look back not even 2 pages and you will see lots of uploading image questions that have been solved with workable code PLUS if you search i remember there being a couple on uploading images to a database but you would need to search as i remember them being a few months back.
Hope this helps and sorry i wont help solve the code you shown, just fed up of the same old questions rather then people searching what has previously been asked.

EDIT: Also you need to make sure you are using the right table type for the database

This post has been edited by Auzzie: 23 January 2010 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 johnd07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Uploading Images into MYSQL Database...

Posted 24 January 2010 - 04:34 PM

sorry my bad, but i'm uploading them to a folder on the server and store the name in the database... i know how to upload one but uploading multiple images is the problem i'm having
Was This Post Helpful? 0
  • +
  • -

#5 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Re: Uploading Images into MYSQL Database...

Posted 24 January 2010 - 04:58 PM

Well why don't you just loop through the $_FILES array? or look at the first page of the tutorials and you would see this one: http://www.dreaminco...wtopic76064.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1