waverider303 Posted December 15, 2009 Share Posted December 15, 2009 Is this possible to do? for($s=0; $s==$file_len; $s++) { // SQL Statements $sql = "INSERT INTO `upload_images` (`id`, `image_name`, `img_size`, `img_medium`, `img_title`, `page_name`, `bio_link`, `img_url`, ) VALUES ('', '$image_name[$s]', '$sizes[$s]', '$mediums[$s]', '$titles[$s]', '$page_name', '$bio_link', '$img_url')"; if(mysql_query($sql)){ echo "hello!"; } } Quote Link to comment https://forums.phpfreaks.com/topic/185263-sql_query-for-loop/ Share on other sites More sharing options...
wildteen88 Posted December 15, 2009 Share Posted December 15, 2009 Yes that is possible. However do note that you can perform more than one insert within a single query. This will be much more efficient: if($file_len > 0) { // start query $sql = 'INSERT INTO `upload_images` (`image_name`, `img_size`, `img_medium`, `img_title`, `page_name`, `bio_link`, `img_url`, ) VALUES '; // generate the rest of the query for($s=0; $s < $file_len; $s++) { $sql .= " ('$image_name[$s]', '$sizes[$s]', '$mediums[$s]', '$titles[$s]', '$page_name', '$bio_link', '$img_url'),"; } // remove ending comma $sql = substr($sql, 0, -1); if(mysql_query($sql)) { echo "Inserted " . count($file_len) . " files successfully"; } else { die( "ERROR! Unable insert files<br />" . mysql_error() ); } } You should also be sanitizing your variables that you use within your queries too. Quote Link to comment https://forums.phpfreaks.com/topic/185263-sql_query-for-loop/#findComment-977974 Share on other sites More sharing options...
waverider303 Posted December 15, 2009 Author Share Posted December 15, 2009 I am receiving an error. I believe it is due to the version of php/mysql that I am running. Here is the error: 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 ('', '', '', '', 'home', '', '../images') ('', '', '', '', 'home', '',' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/185263-sql_query-for-loop/#findComment-977997 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.