Jump to content

Need help inserting data from an Array into a DB


grozanc

Recommended Posts

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).

 

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/>";
}
echo "<input type=submit value=Submit>";

 

// 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 in the array. If anyone can point me in the right direction I'd be forever grateful!

 

Thanks,

Gary

Link to comment
Share on other sites

Change

// 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);
    }
}

 

To

// 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);
    }
}

 

Now change

                        '$filename'
                        '$filename2'
                        '$filename3')";

to

                        '$filename1'
                        '$filename2'
                        '$filename3')";

 

Note: If you are unsure what $$_name meas it is called a variable variable

Link to comment
Share on other sites

Thanks for the help, but unfortunately it didn't work completely. 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 images name doesn't. So basically the name for the last file uploaded is getting lost. Any suggestions?

 

// Post Variables
$challenge = $_POST['challenge'];
$insight = $_POST['insight'];
$oasis_id = $_POST['oasis_id'];
$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()); 

Link to comment
Share on other sites

Hello Again,

 

I don't want to mark this as "SOLVED" because the change I made doesn't seem like it should have worked. But, changing

$i = 0;

to

$i = 1;

allowed the last image name in the array to be inserted into the database. If anyone can say for certain that this code is correct, please let me know!

 

Thanks,

Gary

Link to comment
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.