streamland Posted November 7, 2012 Share Posted November 7, 2012 Could you help me solve the problem The code bellow locate the image path just in "image1" Something like this id image1 image2 image3 image4 image5 image6 1 pic1 2 pic2 but I need to get something like this id image1 image2 image3 image4 image5 image6 1 pic1 pic2 pic2 pic2 pic2 pic2 2 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); Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/ Share on other sites More sharing options...
Muddy_Funster Posted November 8, 2012 Share Posted November 8, 2012 for a start, your $query is being reset on each itteration of the loop. Additionaly, I don't understand from your description or your code exactly what you are trying to perform here, so can't help any more yet... Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1391003 Share on other sites More sharing options...
streamland Posted November 8, 2012 Author Share Posted November 8, 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 have changed some thing in my code and I canget the results like bellow. But now I need a way how to get arround with comma in (image5,) and here with slashes VALUES (" \'Z Could you help me INSERT INTO `imloop` ( image, image1, image2, image3, image4, image5,) VALUES (" \'Z:/home/multiple/www/uploaded_files/1352385065-1.bmp\', \'Z:/home/multiple/www/uploaded_files/1352385065-10.JPG\', \'Z:/home/multiple/www/uploaded_files/1352385065-11.JPG\', \'Z:/home/multiple/www/uploaded_files/1352385065-11_03.jpe\', \'Z:/home/multiple/www/uploaded_files/1352385065-100M2 DUBLEX.jpe\', \'Z:/home/multiple/www/uploaded_files/1352385065-071122_galvanizlicelik08.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/1352385065-1.bmp\', \'Z:/home/' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1391070 Share on other sites More sharing options...
AyKay47 Posted November 8, 2012 Share Posted November 8, 2012 I'm having a hard time following the logic in your code, please give a detailed description of what exactly you are trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1391102 Share on other sites More sharing options...
streamland Posted November 8, 2012 Author Share Posted November 8, 2012 I'm having a hard time following the logic in your code, please give a detailed description of what exactly you are trying to do. I just simple like to arange pic like bellow id image1 image2 image3 image4 image5 image6 1 pic1 pic2 pic2 pic2 pic2 pic2 2 Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1391154 Share on other sites More sharing options...
cyberRobot Posted November 9, 2012 Share Posted November 9, 2012 But now I need a way how to get arround with comma in (image5,)... The extra comma comes from the following code: $query_part_1 .= " image" . $i++ . ","; This just creates a string of column names. It looks like you tried creating an array of column names so you could use the implode() function, but that just results in an array with a single string. Instead, try the following: <?php $cols = array(); foreach($active_keys as $key) { $cols[] = " image" . $i++; } ?> Note that you'll need to get rid of the following line of code, since it's not needed: $cols[] = $query_part_1; Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1391278 Share on other sites More sharing options...
streamland Posted November 13, 2012 Author Share Posted November 13, 2012 (edited) Having some change like bellow it gives me an error INSERT INTO `imloop` ( image1, image2, image3, image4, image5, image6) VALUES (" \'1352838703-1.jpg\', \'1352838703-2.jpg\', \'1352838703-4.jpg\', \'1352838703-5.jpg\', \'1352838703-6.jpg\', \'1352838703-5.jpg\'") Column count doesn't match value count at row 1 $i=1; // 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); $query_part_1 .= " image" . $i++ . ","; $query_part_2 .= " '" . basename($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); $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()); CREATE TABLE IF NOT EXISTS `imloop` ( `id` int(10) NOT NULL AUTO_INCREMENT, `image1` varchar(255) DEFAULT NULL, `image2` int(255) NOT NULL, `image3` int(11) NOT NULL, `image4` int(11) NOT NULL, `image5` int(11) NOT NULL, `image6` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=38 ; Edited November 13, 2012 by streamland Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392121 Share on other sites More sharing options...
AyKay47 Posted November 13, 2012 Share Posted November 13, 2012 The error should be self explanatory, invalid syntax is what is causing the error. Do not wrap the entire values block in quotes. It should read: INSERT INTO `imloop` ( image1, image2, image3, image4, image5, image6) VALUES ('1352838703-1.jpg', '1352838703-2.jpg', '1352838703-4.jpg', '1352838703-5.jpg', '1352838703-6.jpg', '1352838703-5.jpg') Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392158 Share on other sites More sharing options...
cyberRobot Posted November 14, 2012 Share Posted November 14, 2012 First, I think you should simplify. The $query_part_1 and $query_part_2 could be removed. You could change the following: $i=1; foreach($active_keys as $key) { //...move file code here $query_part_1 .= " image" . $i++ . ","; $query_part_2 .= " '" . basename($uploadFilename[$key]). "',"; } $query_part_1 = preg_replace("/,$/","",$query_part_1); $query_part_2 = preg_replace("/,$/","",$query_part_2); $cols[] = $query_part_1; $data[] = $query_part_2; To: $i = 1; $cols = array(); $data = array(); foreach($active_keys as $key) { //...move file code here $cols[] = "image" . $i++; //<-- note that I removed the comma $data[] = basename($uploadFilename[$key]); //<-- note that I removed the quotes } Then just add a space after the comma in the query: //...array_map code here $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ') VALUES ("' . implode('", "', $data) . '")'; //...process query here Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392286 Share on other sites More sharing options...
streamland Posted November 14, 2012 Author Share Posted November 14, 2012 Thank you I will test it now ! Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392292 Share on other sites More sharing options...
streamland Posted November 14, 2012 Author Share Posted November 14, 2012 (edited) Yeh I have tested it and it locate image (to db) as I needed but the problem is now shown bellow : 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), (2, '1352904073-6.jpg', 1352904073, 0, 0, 0, 0), (3, '1352904279-1.jpg', 1352904279, 1352904279, 0, 0, 0), (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 streamland Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392308 Share on other sites More sharing options...
cyberRobot Posted November 14, 2012 Share Posted November 14, 2012 What does your code look like? Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392312 Share on other sites More sharing options...
streamland Posted November 14, 2012 Author Share Posted November 14, 2012 <?php // filename: upload.processor.php // first let's set some variables include("conf.php"); // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); //$db = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // make a note of the current working directory, relative to root. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); // make a note of the directory that will recieve the uploaded files $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/'; // make a note of the location of the upload form in case we need it $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.form.php'; // make a note of the location of the success page $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.success.php'; // name of the fieldname used for the file in the HTML form $fieldname = 'file'; //echo'<pre>';print_r($_FILES);exit; // Now let's deal with the uploaded files // possible PHP upload errors $errors = array(1 => 'php.ini max file size exceeded', 2 => 'html form max file size exceeded', 3 => 'file upload was only partial', 4 => 'no file was attached'); // check the upload form was actually submitted else print form isset($_POST['submit']) or error('the upload form is neaded', $uploadForm); // check if any files were uploaded and if // so store the active $_FILES array keys $active_keys = array(); foreach($_FILES[$fieldname]['name'] as $key => $filename) { if(!empty($filename)) { $active_keys[] = $key; } } // check at least one file was uploaded count($active_keys) or error('No files were uploaded', $uploadForm); // check for standard uploading errors foreach($active_keys as $key) { ($_FILES[$fieldname]['error'][$key] == 0) or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm); } // 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++; } } $i = 1; $cols = array(); $data = array(); // 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); $cols[] = "image" . $i++; //<-- note that I removed the comma $data[] = basename($uploadFilename[$key]); //<-- note that I removed the quotes } $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) . '")'; //$strSQL .="(Thumbnails,FilesName) VALUES ('".$new_images."','".$image_name[$i]."')"; mysql_query($query) or exit($query . '<br />' . mysql_error()); // If you got this far, everything has worked and the file has been successfully saved. // We are now going to redirect the client to the success page. header('Location: ' . $uploadSuccess); mysql_close($connection); // make an error handler which will be used if the upload fails function error($error, $location, $seconds = 5) { header("Refresh: $seconds; URL=\"$location\""); echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n". '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n". '<html lang="en">'."\n". ' <head>'."\n". ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n". ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n". ' <title>Upload error</title>'."\n\n". ' </head>'."\n\n". ' <body>'."\n\n". ' <div id="Upload">'."\n\n". ' <h1>Upload failure</h1>'."\n\n". ' <p>An error has occured: '."\n\n". ' <span class="red">' . $error . '...</span>'."\n\n". ' The upload form is reloading</p>'."\n\n". ' </div>'."\n\n". '</html>'; exit; } // end error handler ?> Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1392391 Share on other sites More sharing options...
streamland Posted November 26, 2012 Author Share Posted November 26, 2012 I appreciate for help I could get it work. The path goes to db and the I can weiw 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 here Could 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 filename foreach($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++;[/background][/size][/font][/color] [color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3][background=rgb(250, 250, 250)] $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); // получаем ширину $photoY = ImagesY($images_orig); // а здесь высоту $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) . ') [/background][/size][/font][/color] [color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3][background=rgb(250, 250, 250)] VALUES (' . implode(', ', $data) . ' , ' . implode(', ', $data_sm) . ')'; Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395292 Share on other sites More sharing options...
cyberRobot Posted November 26, 2012 Share Posted November 26, 2012 What do we need to look at? Is it still having a problem where the image data doesn't contain the full file name (Reply 11)? If so, what does $query look like when it's displayed to the screen? Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395327 Share on other sites More sharing options...
Christian F. Posted November 27, 2012 Share Posted November 27, 2012 I would recommend you to redesign the database, so that you have a designated table for the images. Where you store one image per row. You'll also need a table for the image loop settings, naturally enough, and if you might be using the same image in several loops: A table for the loop-image relation (many-to-many relation). If there is no chance of using the same image in several loops, then you can save the image loop ID into the image table. Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395431 Share on other sites More sharing options...
streamland Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) What do we need to look at? Is it still having a problem where the image data doesn't contain the full file name (Reply 11)? If so, what does $query look like when it's displayed to the screen? The code works fine untill I am adding thambnail code $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ') VALUES ("' . implode('", "', $data) . '")'; This gives me something like this (image1,image2,image3,image4,image5,image6) values (23423423-1.jpeg,32423423-2.jpeg,9798796-3.jpeg,123123123-4.jpeg,2342342323-5.jpeg,23423423-6.jpeg) and it locate the image the way I need. I just need to implement the thambnail code and trying to built something like this (image1,image2,image3,image4,image5,image6,imagesm1,imagesm2,imagesm3,imagesm4,imagesm5,imagesm6) $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ' , ' . implode(', ', $cols_sm) . ') VALUES (' . implode(', ', $data) . ' , ' . implode(', ', $data_sm) . ')'; I would recommend you to redesign the database, so that you have a designated table for the images. Where you store one image per row. You'll also need a table for the image loop settings, naturally enough, and if you might be using the same image in several loops: A table for the loop-image relation (many-to-many relation).If there is no chance of using the same image in several loops, then you can save the image loop ID into the image table. Thank you for given info but I'd like to finish this code. Edited November 27, 2012 by streamland Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395443 Share on other sites More sharing options...
cyberRobot Posted November 27, 2012 Share Posted November 27, 2012 You could merge the column and value arrays before executing the query with array_merge(). That way, you would only need one implode. $cols = array_merge($cols, $cols_sm); $data = array_merge($data, $data_sm); $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ') VALUES (' . implode(', ', $data) . ')'; Also, the values will probably need to be enclosed in quotes (see Reply 9 for an example). Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395516 Share on other sites More sharing options...
mrMarcus Posted November 27, 2012 Share Posted November 27, 2012 (edited) The code works fine untill I am adding thambnail code $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ') VALUES ("' . implode('", "', $data) . '")'; This gives me something like this (image1,image2,image3,image4,image5,image6) values (23423423-1.jpeg,32423423-2.jpeg,9798796-3.jpeg,123123123-4.jpeg,2342342323-5.jpeg,23423423-6.jpeg) and it locate the image the way I need. I just need to implement the thambnail code and trying to built something like this (image1,image2,image3,image4,image5,image6,imagesm1,imagesm2,imagesm3,imagesm4,imagesm5,imagesm6) $query = 'INSERT INTO `imloop` (' . implode(', ', $cols) . ' , ' . implode(', ', $cols_sm) . ') VALUES (' . implode(', ', $data) . ' , ' . implode(', ', $data_sm) . ')'; Thank you for given info but I'd like to finish this code. But your 'storing of images' logic is wrong. Finishing this code, unless it's being specifically requested by a client, is a waste of time. Create an `images` table and store all images in there. Then, join the table when necessary. Edited November 27, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/270433-send-data-using-foreach/#findComment-1395519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.