Jump to content

Recommended Posts

I have a multiple image up loader to upload images to a server. Now while it uploads the file I want it the insert the link to the image in the database. What I have works, but not exactly how I want.

 

I have a table called hpics with 10 fields, 1 of them is called id, which in turn is not auto_inceremented, because I want to insert the id. Now, I'm trying the insert all 9 images, when uploaded, with 1 row with an id. Instead it uploads the images as seperate rows, instead of the fields

 

This is my code:

while(list($key,$value) = each($_FILES['pictures']['name']))
	{
		if(!empty($value))
			{   
				$filename = $value;
				$add = "upload/$filename";

                   if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key], $add))
			   		{
						$i = 1;
						$sql = "INSERT INTO hpics SET p".$i." = '".$add."'";

						$insert = mysql_query($sql) or die("<b>Error</b>: " . mysql_error());
						++$i;
						}
			}
	}

You may want to clarify exactly what you need.

 

Becuase it sounds like you just need to create 2 tables. One for images and URL, and one for the rest of the data.

You could get fancy and use InnoDB tables and enforce a foreign key and all that stuff.

 

That is, if you want a seperate URL per image.

 

You want all the images in the same row, per submission, and the URL of a page that displays the multiple images? (seemed like this was your goal) If you are uploading the whole data (not just name, but every bit of information, you'll have to have multiple fields - as many as you allow images to be uploaded - but be warned that the database could get bogged down and huge in a short time. It'll just means a lot of fields have no data if they just upload one pictures.

 

If you want to keep it all in a single field, then you could just strip that array as comma separated or space separated, and call them all (granted that you upload them to your server and NOT the SQL database)

 

 

Well. I'm building a CMS system for a realestate site, and want the admin, to upload the data, and the images in one try.

 

I do have 2 table, one for data and one for pictures. What i'm trying to get is that, when the data gets inserted into the data table, it's ID is auto_incremented. I'll get that ID #, just inserted, with LAST_INSERT_ID() and then insert all the images url, into the row asigned the ID i just got.with the technique described here http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

 

INSERT INTO foo (auto,text)
    VALUES(NULL,'text');         # generate ID by inserting NULL
INSERT INTO foo2 (id,text)
    VALUES(LAST_INSERT_ID(),'text');  # use ID in second table

 

just, with the script I have, it inserts every image in it's own row, instead of 1 row with all fields p1-p9

.

Or is it just easier to assign each their own row, with the ID??

The script you have above only talks about 1 table, the image one.

And it is creating a row per image, so it's probably easier to leave it as is, 1 row per image. Obviously not the most efficient method, but isn't it already doing what you need it to do then?

 

You could change your code by creating a string/array variable that contains all the uploaded images, then pass that into the query.

So before your SQL query, have a variable that is storing every image, then run the query.

The script you have above only talks about 1 table, the image one.

And it is creating a row per image, so it's probably easier to leave it as is, 1 row per image. Obviously not the most efficient method, but isn't it already doing what you need it to do then?

 

You could change your code by creating a string/array variable that contains all the uploaded images, then pass that into the query.

So before your SQL query, have a variable that is storing every image, then run the query.

 

Yeah it tallks about only one, since i'll be running this query after the 1st data one has finished, but i'm only using this one now, since I want to make sure I got this part, before I move on.

 

I have no Idea how I would creat an array from a multiupload file

This is the code I have so far for uploading the data AND the pictures.

<?php 

include 'dbcon.php';

$name 	= $_POST['name'];
$lname 	= $_POST['lname'];
$type	= $_POST['type'];
$address= $_POST['address'];
$city	= $_POST['city'];
$area	= $_POST['district'];	
$dscpt	= $_POST['dscpt'];
$bedr	= $_POST['bedr'];
$bathr	= $_POST['bathr'];
$story	= $_POST['story'];
$sqft	= $_POST['sqft'];
$yearb	= $_POST['yearb'];
$status	= $_POST['status'];
$awg	= $_POST['awg'];
$usd	= $_POST['usd'];
$floor	= $_POST['flooring'];
$cooling= $_POST['cooling'];
$garage = $_POST['garage'];
$swpool = $_POST['swpool'];

if( empty($type) && empty($status) )
	{
		echo "Please Fill in the type/status fields";
		die();
	}

$sql = "INSERT INTO house SET 	name='".$name."',
								lastn='".$lname."',
								usd='".$usd ."',
								price_AWG='".$awg."',
								address='".$address."',
								area='".$area."',
								sqft='".$sqft."',
								yearb='".$yearb."',
								status='".$status."',
								type='".$type."',
								story='".$story."',
								bedr='".$bedr."',
								bathr='".$bathr."',
								flooring='".$floor."',
								cooling='".$cooling."',
								garage='".$garage."',
								swpool='".$swpool."',
								dispr='".$dscpt."'";

$insert = mysql_query($sql) or die("<b>Error</b>: " . mysql_error());
$id = mysql_insert_id();	

foreach ( $_FILES["pictures"]["error"] as $key => $error ) 
	{
		if ( $error == UPLOAD_ERR_OK ) 
			{
				$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       				 $name = $_FILES["pictures"]["name"][$key];
				 $path = "upload/$name";
        			
				if(move_uploaded_file($tmp_name, $path))
					{
						$sql = "INSERT INTO hpics SET id='".$id."', p1='".$path."'";
						$result = mysql_query($sql) or die("Error");
						echo "<img src=\"".$path."\" alt=\"\" /><br />";
					}
		}
	}

?>

 

For some reason the picture uploader works fine when it's on its own, but when I use the script above, it inserts the data, but does nothing with the pictures. I don't get it.

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.