If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Sintaks error when i use foreach

The code below gives a sintaks error when I'am trying to send path to the db!
Could you please help me solve the problem

Error:

INSERT INTO imloop (', 'image1'') VALUES (','Z:\tmp\phpB8.tmp'')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', 'image1'') VALUES (','Z:\tmp\phpB8.tmp'')' at line 1

INSERT INTO `imloop` ( image1 image2) VALUES ( 'Z:/home/multiple/www/uploaded_files/1352231075-1.jpg' 'Z:/home/multiple/www/uploaded_files/1352231075-2.jpg')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'image2) VALUES ( 'Z:/home/multiple/www/uploaded_files/1352231075-1.jpg' 'Z:/home' at line 1

// check that the file we are working on really was an HTTP upload
foreach($active_keys as $key)
{
@is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
}

// validation... since this is an image upload script we
// should run a check to make sure the upload is an image
foreach($active_keys as $key)
{
@getimagesize($_FILES[$fieldname]['tmp_name'][$key])
or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
}

// make a unique filename for the uploaded file and check it is
// not taken... if it is keep trying until we find a vacant one
foreach($active_keys as $key)
{
$now = time();
while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
{
$now++;

}
}

// now let's move the file to its final and allocate it with the new filename
foreach($active_keys as $key)
{
@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
or error('receiving directory insuffiecient permission', $uploadForm);

Without knowing exactly what's in the $uploadFilename and $active_keys arrays, I had to make a few assumptions, but hopefully with what's here, you can make adjustments as needed.

You can ignore the kdo() functions, those are my debugs (look at the print_r() results below them)

PHP Code:

// I'm making up the $uploadFilename and $active_keys arrays; based on your code, I think this is how they look

// this is just a zero-based array of the file paths
// !! this won't be in your final code !!
$uploadFilename = array("/path/to/file1.jpg", "/path/to/file2.jpg", "/path/to/file3.jpg", "/path/to/file4.jpg", "/path/to/file5.jpg", "/path/to/file6.jpg");

// now let's move the file to its final and allocate it with the new filename
// as a safety net, I always unset the $key=>$value of a foreach loop prior to using them {I also always use $key=>$value, and not just $value}
unset($key, $image);
foreach($active_keys as $key => $image)
{
@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
or error('receiving directory insuffiecient permission', $uploadForm);

You don't need to loop through $array_keys FOUR TIMES... rather, add all those sanity checks into one loop, and in reality, could probably even be added to the whole "write the db query" loop as well, but that's for a later enhancement.

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);

// validation... since this is an image upload script we
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'][$key])
or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);

// make a unique filename for the uploaded file and check it is
// not taken... if it is keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
{
$now++;
}