Help insert info from an Array into a DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grozanc
Forum Newbie
Posts: 14
Joined: Tue Oct 23, 2007 6:50 pm

Help insert info from an Array into a DB

Post by grozanc »

Hello All,

First, I'm new to php and I have spent a lot of time searching forums and google for the answer to my question with no luck, so if it's a topic already covered, PLEASE reply with a link or point me in the right direction.

OK, I have a form that let's users upload multiple images. The upload portion works fine. What I can't figure out how to do is put the image name of each file that gets uploaded all into the same table record. I also need each file name to be put into it's own column in the table. For example, file1, file2 and file3 will get dumped into the same record and into it's respective column (uploaded_image1, uploaded_image2, uploaded_image3). I've included the working upload code and the code that inserts some of the information into the database (just not the image names).

Code: Select all

echo "<textarea name=challenge rows=15 cols=60> </textarea><br/><br/>"
echo "<textarea name=insight rows=15 cols=60> </textarea><br/><br/>"
for($i=1; $i<=$max_no_img; $i++){
echo "Image $i
<input type=file name='image[]' ><br/>";
}

Code: Select all

// Connect to DB
$host = 'xxx';
$user = 'xxx';
$pass = 'xxx';
$db = 'xxx';
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

// Post Variables
$challenge = $_POST['challenge'];
$insight = $_POST['insight'];
$filename=serialize($_POST['filename']);

// Upload Images
while(list($key,$value) = each($_FILES[image][name]))
{
    if(!empty($value))
    {   
        $filename = $value;
        $add = "upimage/$filename";
        copy($_FILES[image][tmp_name][$key], $add);
        chmod("$add",0777);
    }
}

// Insert into database
$query = "INSERT INTO table (
			challenge,
			insight,
                        uploaded_image1,
                        uploaded_image2,
			uploaded_image3,)".
			
		"values (
			'$challenge',
			'$insight',
                        '$filename'
                        '$filename2'
			'$filename3')";
			
mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

?>
I know just enough about php that the reason the above wont work, is because $filename2 and $filename3 haven't been given a value. Unfortunately, I have no idea how to separate that information up in the array. If anyone can point me in the right direction I'd be forever grateful!

Thanks,
Gary
grozanc
Forum Newbie
Posts: 14
Joined: Tue Oct 23, 2007 6:50 pm

Re: Help insert info from an Array into a DB

Post by grozanc »

I think I've gotten a little closer but now if I upload one image, the image name doesn't get stored in the database, if I upload two images only the first image name goes into the database. If I upload three images, the first two names get stored, and the last image's name doesn't. So basically the name for the last file uploaded is getting lost. Any suggestions? Here is my most current attempt.

Code: Select all

// Post Variables
$challenge = $_POST['challenge'];
$insight = $_POST['insight'];
$filename=serialize($_POST['filename']);

// Upload Images
$i = 0;
while(list($key,$value) = each($_FILES['image']['name']))
{
    $_name = 'filename' . $i++;
    $$_name = '';

    if(!empty($value))
    { 
        $$_name = $value;
        $add = 'upimage/'.$$_name;
        copy($_FILES['image']['tmp_name'][$key], $add);
        chmod($add, 0777);
    }
}

// Insert into database
$query = "INSERT INTO table (
			oasis_id,
			challenge,
			insight,
			uploadedfile1,
			uploadedfile2,
			uploadedfile3 )".
			
		"values (
			'$oasis_id',
			'$challenge',
			'$insight',
			'$filename1',
			'$filename2',
			'$filename3')";
			
mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

Post Reply