vindesigns Posted August 10, 2011 Share Posted August 10, 2011 Hi, I wonder if you guys could help me. I have a table called modules and within it it has moduleID, moduleTitle, moduleCode etc. A checkbox is populated next to each module that is displayed and the value of the checkbox is the moduleID. When a user checks the modules and click submit they get passed onto another page. On this other page I have managed to retrieve the moduleID's which have been checked but I am having trouble in turning these moduleID so that I can display the moduleTitle, moduleCode etc for the ones that have been checked on this new page. I have used the following code but no luck <?php $cats_upload = ""; foreach($_POST['module'] as $cats) // errors if no modules are selected { echo '<p>Module data moduleID: '.$cats.'</p>'; } $sql = mysql_query("SELECT * FROM modules WHERE moduleID ='".$cats."'"); $productCount = mysql_num_rows($sql); //count the output amount if($productCount > 0){ while($row = mysql_fetch_array($sql)){ $moduleTitle = $row["moduleTitle"]; $cats_upload .= "<tr><td>$moduleTitle</td></tr>"; } } else { $cats_upload = "<tr><td>No modules have been selected</td></tr>"; } ?> If anyone has any suggestions to what it might be, please let me know. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/244388-retrieving-the-passed-id/ Share on other sites More sharing options...
Morg. Posted August 10, 2011 Share Posted August 10, 2011 This could not work .. your $cats is defined only inside the foreach loop <?php $cats_upload = ""; $cats_sql_match_string foreach($_POST['module'] as $cats) // errors if no modules are selected { echo '<p>Module data moduleID: '.$cats.'</p>'; $cats_sql_match_string.=$cats.","; } $cats_sql_match_string=substr($cats_sql_match_string, 0,strlen($cats_sql_match_string)-1); // the mysql extension to PHP is full of problems, you might want to switch to mysqli // you might also want to centralize all your queries into one function query($stuff) in order to avoid maintenance nightmares (as in switching from a db extension to another) $sql = mysql_query("SELECT * FROM modules WHERE moduleID IN'".$cats_sql_match_string."'"); // Besides, executing a query with potentially no $cats at all is not a good idea, add some checks for that $productCount = mysql_num_rows($sql); //count the output amount if($productCount > 0){ while($row = mysql_fetch_array($sql)){ Quote Link to comment https://forums.phpfreaks.com/topic/244388-retrieving-the-passed-id/#findComment-1255301 Share on other sites More sharing options...
vindesigns Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks for your response Morg, What would be the best way to re-write the foreach loop, for example to retrieve the moduleTitle? as I'm still new to PHP. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/244388-retrieving-the-passed-id/#findComment-1255344 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.