Stream Posted November 6, 2012 Share Posted November 6, 2012 foreach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $query_part_1 = '';$query_part_2 = '';$i = 1; // add the comma AFTER you write the key/values to the strings$query_part_1 .= " image" . $i++ . ",";$query_part_2 .= " '" . $uploadFilename[$key]. "',"; $query_part_1 = preg_replace("/,$/","",$query_part_1); // remove the comma at the end of the string$query_part_2 = preg_replace("/,$/","",$query_part_2);$query = "INSERT INTO `imloop` (" .$query_part_1. ") VALUES (" .$query_part_2. ")"; mysql_query($query) or exit($query . '<br />' . mysql_error()); } mysql_close($connection); The code above gives something like below id image1 image2 image3 image4 image5 image6 1 pic12 pic23 pic2 Something like this but I need to get something like this id image1 image2 image3 image4 image5 image6 1 pic1 pic2 pic3 pic4 pic5 pic62 Could you help me please create something like this. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 6, 2012 Share Posted November 6, 2012 You're saying there's a problem with that insert query? What do you see if you print out the query before you run it? Running that query should probably go outside of the loop, you build the query in the loop and then run the query once the loop finishes. Link to comment Share on other sites More sharing options...
Stream Posted November 6, 2012 Author Share Posted November 6, 2012 foreach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $query_part_1 = '';$query_part_2 = '';$i = 1; // add the comma AFTER you write the key/values to the strings$query_part_1 .= " image" . $i++ . ",";$query_part_2 .= " '" . $uploadFilename[$key]. "',"; } I have taken out this part of script but that doesn't give any result ! In this case it just put one path. $query_part_1 = preg_replace("/,$/","",$query_part_1); // remove the comma at the end of the string$query_part_2 = preg_replace("/,$/","",$query_part_2);$query = "INSERT INTO `imloop` (" .$query_part_1. ") VALUES (" .$query_part_2. ")"; mysql_query($query) or exit($query . '<br />' . mysql_error());mysql_close($connection); Link to comment Share on other sites More sharing options...
thescientist Posted November 6, 2012 Share Posted November 6, 2012 it's going to take a little more than just moving it outside of the for loop. Mostly because every iteration of the for loop sets the query part string back to an empty string. So you would also want to move those variable initializations outside of the loop, but before it. After that, if there are issue, you should start by echo'ing the queries each time the loop runs to make sure it's being built up correctly. And echo out the final query before you run it, etc. Link to comment Share on other sites More sharing options...
Stream Posted November 7, 2012 Author Share Posted November 7, 2012 I have chaned the code like bellow but it gives me an error foreach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $cols[] = $query_part_1; $data[] = $query_part_2; $query_part_1 .= " image" . $i++ . ",";$query_part_2 .= " '" . $uploadFilename[$key]. "',"; $cols = array_map ('mysql_real_escape_string', $cols); $data= array_map ('mysql_real_escape_string', $data); $query = 'INSERT INTO `imloop` (' . implode(',', $cols) . ') VALUES ("' . implode('","', $cols) . '")'; } mysql_query($query) or exit($query . '<br />' . mysql_error()); mysql_close($connection); INSERT INTO `imloop` (, image,, image, image1,, image, image1, image2,, image, image1, image2, image3,, image, image1, image2, image3, image4,) VALUES (""," image,"," image, image1,"," image, image1, image2,"," image, image1, image2, image3,"," image, image1, image2, image3, image4,")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 ' image,, image, image1,, image, image1, image2,, image, image1, image2, image3,,' at line 1 Link to comment Share on other sites More sharing options...
Ingolme Posted November 7, 2012 Share Posted November 7, 2012 Given your code, one would assume that you're trying to add data to a table with unlimited fields. How many fields does the table have? image1, image2 ... imageN? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2012 Share Posted November 7, 2012 You keep doubling everything. You have these lines: $cols[] = $query_part_1;$data[] = $query_part_2;$query_part_1 .= " image" . $i++ . ",";$query_part_2 .= " '" . $uploadFilename[$key]. "',"; The first time through the loop both query parts are empty, so you add empty strings to the cols and data arrays. The next time through you add the query parts from the last loop to the array, and then add more to the queries. So the arrays start looking like this, for $cols: Array( 0 => '', 1 => 'image,', 2 => 'image, image1,', 3 => 'image, image1, image2,', 4 => 'image, image1, image2, image3,', ...) Then when you implode that array you end up with this: INSERT INTO `imloop` (, image,, image, image1,, image, image1, image2,, image, image1, image2, image3,, image, image1, image2, image3, image4,) There's the empty string there, then just "image," (which you add another comma after because of the implode), then "image, image1,", etc, for each item in the array. You're duplicating everything by adding everything to a string and adding each string to an array, then imploding the array. Pick one of them. Either add columns and values to an array and then implode the array, or add them to a string. Don't add them to a string and then add the string to an array or you're going to end up with what you have, which obviously doesn't work for SQL. You also run mysql_real_escape_string every time through the loop. If you had a quote in there it would keep adding slashes before it every time you used that function, not just once. You need to separate things that need to be run inside the loop (gathering the columns and values) with things that go outside the loop (escaping data, building the final query). Link to comment Share on other sites More sharing options...
Stream Posted November 7, 2012 Author Share Posted November 7, 2012 Given your code, one would assume that you're trying to add data to a table with unlimited fields. How many fields does the table have? image1, image2 ... imageN? I am planing to have about 10 (imag1,image2,image3,image4,image5,imag6........image10) Link to comment Share on other sites More sharing options...
Stream Posted November 7, 2012 Author Share Posted November 7, 2012 foreach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $query_part_1 .= " image" . $i++ . ",";$query_part_2 .= " '" . $uploadFilename[$key]. "',"; } $cols[] = $query_part_1;$data[] = $query_part_2; $cols = array_map ('mysql_real_escape_string', $cols);$data= array_map ('mysql_real_escape_string', $data);$query = 'INSERT INTO `imloop` (' . implode(',', $cols) . ') VALUES ("' . implode('","', $data) . '")';mysql_query($query) or exit($query . '<br />' . mysql_error()); mysql_close($connection); I've puted out a part of code and could get some result INSERT INTO `imloop` ( image, image1, image2, image3, image4, image5,) VALUES (" \'Z:/home/multiple/www/uploaded_files/1352319971-1.jpg\', \'Z:/home/multiple/www/uploaded_files/1352319971-2.jpg\', \'Z:/home/multiple/www/uploaded_files/1352319971-3.jpg\', \'Z:/home/multiple/www/uploaded_files/1352319971-5.jpg\', \'Z:/home/multiple/www/uploaded_files/1352319971-6.jpg\', \'Z:/home/multiple/www/uploaded_files/1352319971-5.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 ') VALUES (" \'Z:/home/multiple/www/uploaded_files/1352319971-1.jpg\', \'Z:/home/' at line 1 I think i should find a way to escape comma (image5,) and also VALUES (" \' Could you help me solve it please Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2012 Share Posted November 7, 2012 You don't escape that trailing comma, you remove it. You had code previously to remove the trailing comma. As far as escaping the data goes, you're still using both a string and an array. Use one or the other. If you want to build it in a string then use mysql_real_escape_string when you add the value to the query inside the loop. If you want to add the values to an array and then implode it later then you would use array_map like you're doing outside the loop. But don't do both, there's no reason to build a query string, then add that string to an array and run array_map on it (there's only one item in the array, the entire string), then implode the array. Either build the string in the loop, or add the values to an array in the loop and then implode it later. If you're using the strings you don't need to do anything with an array. Link to comment Share on other sites More sharing options...
Stream Posted November 14, 2012 Author Share Posted November 14, 2012 (edited) I have tested it and it locate image (to db) as I needed but the problem is now shown bellow : $i = 1;$cols = array();$data = array();// now let's move the file to its final and allocate it with the new filenameforeach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $cols[] = "image" . $i++; $data[] = basename($uploadFilename[$key]);} $cols = array_map ('mysql_real_escape_string', $cols);$data= array_map ('mysql_real_escape_string', $data); $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ') VALUES ("' . implode('", "', $data) . '")'; mysql_query($query) or exit($query . '<br />' . mysql_error()); INSERT INTO `imloop` (`id`, `image1`, `image2`, `image3`, `image4`, `image5`, `image6`) VALUES(1, '1352903903-1.jpg', 1352903903, 1352903903, 1352903903, 1352903903, 1352903903); or INSERT INTO `imloop` (`id`, `image1`, `image2`, `image3`, `image4`, `image5`, `image6`) VALUES(1, '1352903903-1.jpg', 1352903903, 1352903903, 1352903903, 1352903903, 1352903903), // here I tried to upload 6 different images(2, '1352904073-6.jpg', 1352904073, 0, 0, 0, 0), // here I tried to upload 2 different images(3, '1352904279-1.jpg', 1352904279, 1352904279, 0, 0, 0), // here I tried to upload 3 different images(4, '1352904587-1.jpg', 1352904587, 1352904587, 0, 0, 0); why it locate just first image I am putting in and repeat it to the `image2`, `image3`, `image4`, `image5`, `image6` .and doesn't show others I am adding. Edited November 14, 2012 by Stream Link to comment Share on other sites More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 You're getting filenames from $uploadFilename, I don't see where you're building that. That's where the filenames are coming from. Link to comment Share on other sites More sharing options...
Stream Posted November 26, 2012 Author Share Posted November 26, 2012 (edited) I appreciate for help I could get it work. The path goes to db and the I can veiw it. But also I'd like by the way thambnail the images and locate path the same way .image1 image2 image3 image4 image5 image6 image_small1 image_small2 image_small3 image_small4 image_small5 image_small6 I've got the thambnail script which I would like to implement hereCould you please help me do it $i = 1;$sm = 1;$images_sm = "thumbnails_"$uploadFilename[$key];// now let's move the file to its final and allocate it with the new filenameforeach($active_keys as $key){@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key],$uploadFilename[$key]) or error('receiving directory insuffiecient permission', $uploadForm); $cols[] = "image" . $i++; //<-- note that I removed the comma $cols_sm[] = "image_small" . $sm++; $data[] = "'" . basename($uploadFilename[$key]) . "'"; //----------------------------------------------------------------------------------------------------- $width=100; //*** Fix Width & Heigh (Autu caculate) ***// //$size=GetimageSize($images); $size=GetimageSize($uploadFilename[$key]); $height=round($width*$size[1]/$size[0]); $images_orig = ImageCreateFromJPEG($uploadFilename[$key]); $photoX = ImagesX($images_orig); // Widht $photoY = ImagesY($images_orig); // Hight $images_fin = ImageCreateTrueColor($width, $height); ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); ImageJPEG($images_fin,"uploaded_files/".$images_sm); ImageDestroy($images_orig); ImageDestroy($images_fin); // -------------------------------------------------------------------------------------------------------------- $data_sm[] = "'" . basename($uploadFilename[$key]) . "'"; }$query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ' , ' . implode(', ', $cols_sm) . ') VALUES (' . implode(', ', $data) . ' , ' . implode(', ', $data_sm) . ')'; Edited November 26, 2012 by Stream Link to comment Share on other sites More sharing options...
justsomeguy Posted November 26, 2012 Share Posted November 26, 2012 This line isn't correct: $images_sm = "thumbnails_"$uploadFilename[$key]; You're missing a "." operator there. Other than that, it's hard for me to understand you and it's hard to help with that code without seeing the rest of it, like the contents of $uploadFilename and $active_keys. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now