Help insert info from an Array into a DB
Posted: Sat Sep 04, 2010 2:23 pm
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).
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
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());
?>
Thanks,
Gary