merylvingien Posted October 4, 2009 Share Posted October 4, 2009 Hi guys, been floating about here for the last few days reading and learning, but i could really do with some help here. I have a page with a list of results from my database, each result has a checkbox next to it with a value. When a user selects a checkbox or several checkboxes i need to then pass those values to the next page and check the database for matches against those values. Lets say for arguments sake that the values are 1, 5, 10 , 35 They could be ID's. I need to check if 1, 5, 10, 35 exist in the databse and if they do i need to return the rows of data on the page. For example: you have selected id 1, bla bla bla id 5, bla bla bla id 10, bla bla bla id 35, bla bla bla and so on. I have a little more to ask after i get this sorted, but not too much Any help here would be great. P.s this forum is great, so many people willing to help out, much better than the last i visited Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/ Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 Sounds like your looking for something like this... <input type="checkbox" name="id[]" value="5" /> <input type="checkbox" name="id[]" value="10" /> <input type="checkbox" name="id[]" value="15" /> <?php if(isset($_POST['id'])) { foreach($_POST['id'] as $item) { $sql = "SELECT * FROM table WHERE id=$item LIMIT 1"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); // do something with item } } ?> Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930128 Share on other sites More sharing options...
merylvingien Posted October 4, 2009 Author Share Posted October 4, 2009 WOW thanks for the fast reply, i am getting a bit of an error with that code. Fatal error: SQL: SELECT * FROM table WHERE id=x LIMIT 1, ERROR: Unknown column 'x' in 'where clause' in C:\Program Files\EasyPHP5.3.0\www\file\code7.php on line 94 Cant understand why its saying that, cause its just pulled column x from the database LOL Whats the limit1 about? Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930136 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 LIMIT 1 means only fetch 1 row from the database, as you are searching by unique id you should only ever match one variable it's just a way of potentially speeding up the. I'm not certin it's required when searching a Unique field I just thought I'd add it in there. Your getting the error because you passed x. My code assumed the id is an integer. Strings passed to MySQL should be enclosed in single quotes. Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930147 Share on other sites More sharing options...
merylvingien Posted October 4, 2009 Author Share Posted October 4, 2009 Righto, got it working. MANY MANY MANY thanks goes to cags God i am thick at this... Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930170 Share on other sites More sharing options...
merylvingien Posted October 4, 2009 Author Share Posted October 4, 2009 Ok next question, will be the last for today i promise Ive got my results showing up now as expected, But i have a price for each one. Now to the realy dim part of the question, how do i add these prices up and have a total at the bottom? Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930174 Share on other sites More sharing options...
merylvingien Posted October 4, 2009 Author Share Posted October 4, 2009 Can anyone help me out with this code? Its only displaying the last price total when it should be adding all the prices up to give a final total, ive tried allsorts to get it to work but i cant figure it out. <?php if(isset($_POST['selected'])) { foreach($_POST['selected'] as $item) { $sql = "SELECT * FROM Postcode WHERE postcodeID=$item"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); $total = 0; while ($db_field = mysql_fetch_assoc($result)) { print "<div class='redisplay'>" . $db_field['postcodename']. ", ". $db_field['town']. " <a href='#'>Towns Covered<span> {$db_field['stowns']}</span></a> ". $db_field['county']. ", ". $db_field['country']. " £". $db_field['priceb']. "</div> <br />"; $total += $db_field['priceb']; } } } print " Total = £$total <br />"; mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/176455-passing-checkbox-values-to-next-page-then-query-database-for-matches/#findComment-930247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.