conork Posted September 23, 2011 Share Posted September 23, 2011 Hi all, I hope someone can help me out here as I think I am close but am not sure where to go from here. The problem is: I have a cms where a user can see a list of photos in the gallery. I provide them the ability to select what photos they want to appear on a particular webpage. The problem I have is that the user wants to be able to put the photos in whatever order they choose. So, I have a form (see attached), the html code is: <table width="566px" cellspacing="0" border="0"> <tr> <td width="33%"> <div class="serviceCell"> <input type='checkbox' checked='checked' name='photos[][0]' value='119' /> Bathroom Sin... <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value="0"/> </div> </td> <td width="33%"> <div class="serviceCell"> <input type='checkbox' checked='checked' name='photos[][0]' value='113' /> Courtyard 1 <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value="0"/> </div> </td> <td width="33%"> <div class="serviceCell"> <input type='checkbox' name='photos[][0]' value='114' /> Courtyard 2 <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[][1]" value=""/> </div> </td> </tr>... Now, lets assume that the user ticked the box next to 'Courtyard 2' and entered an integer '1' in the input box to signify that this image should be displayed 1st on the chosen webpage. These 2 values go into the multidimensional array (photoid, ranking) and are submitted in the form to php in the photos[][] array. But here is where I have the problem. I am trying to extract the values from the array and then do an insert to a table in mysql. My syntax must be wrong somewhere so I am asking anyone to please offer any advice: if (isset($_REQUEST['submitted'])){ $photos = $_REQUEST['photos']; ... if(!empty($photos)){ foreach($photos as $photoID){ $sql = "INSERT IGNORE INTO menuItemPhotos SET photoid='$photoID[0]', menuItemid='$id', ranking='$photoID[1]'"; $ok = @mysql_query($sql); // execute the query } // End of foreach($photos as $photoID){ } // End of if(!empty($photos)){ Thanks Conor [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/247743-extracting-data-from-a-multidimensional-array-and-populating-db-with-the-values/ Share on other sites More sharing options...
xyph Posted September 24, 2011 Share Posted September 24, 2011 You're suppressing errors and not making sure mysql_query() didn't return false. Are you surprised you aren't seeing errors? Echo your queries. Are they populated and formatted as you'd expect? If so, verify that mysql_query() returned true. If it didn't, you should echo mysql_error(). Quote Link to comment https://forums.phpfreaks.com/topic/247743-extracting-data-from-a-multidimensional-array-and-populating-db-with-the-values/#findComment-1272284 Share on other sites More sharing options...
Buddski Posted September 24, 2011 Share Posted September 24, 2011 The names of your checkboxes and input fields are all messed up. If you view your POST data you will see. For a better way to name them I would use this.. <div class="serviceCell"> <input type='checkbox' checked='checked' name='photos[119][]' value='1' /> Bathroom Sin... <img src="./images/help.gif" title="Enter an integer to signify the order in which this image will appear on the webpage." alt="Enter an integer to signify the order in which this image will appear on the webpage."/> <input type="text" size="2" name="photos[119][]" value="0"/> </div> Then in PHP foreach($photos as $photoID=>$values){ // $values[0] can be ignored as the ID is the array key // $sql = "INSERT IGNORE INTO menuItemPhotos SET photoid='$photoID', menuItemid='$id', ranking='$value[1]'"; $ok = mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/247743-extracting-data-from-a-multidimensional-array-and-populating-db-with-the-values/#findComment-1272286 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.