EATON106 Posted September 4, 2009 Share Posted September 4, 2009 Hi, I'm new to this stuff and need some help please. Basically I have a MySQL table with a field called TYPES and the information sorted in here is in the following format: PIZZA, PASTA, SALAD, etc... I would like to create a form that would give the user the option of checkboxs to select what they are looking for eg: pizza. My question is, how do I get the info from the form into a MySQL query. Below is my code without the form. Thanks in advance. <?php $con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("infostore", $con); $result = mysql_query("SELECT * FROM storedata where TYPE = pizza"); while($row = mysql_fetch_array($result)) { echo "<div class=info>"; echo "<div class=box>"; echo $row['TYPE']; echo "</div>"; echo "</div>"; } mysql_close($con); ?> Oh, and I'm using MySQL 5.0. Quote Link to comment https://forums.phpfreaks.com/topic/173086-form-to-pull-certain-data-from-mysql-db/ Share on other sites More sharing options...
cbolson Posted September 5, 2009 Share Posted September 5, 2009 Hi, Firstly I presume that you have an "id" field in the storedata table that is unique to each food type. This is what you should use to identify the various types rather than it's string value. For the form you create an array of food types (using the id) something like this: // get food types $list_types=''; $result=mysql_query("SELECT ID, TYPE FROM storedata"); while($row=mysql_fetch_assoc($result)){ $list_types.='<input type="checkbox" name="foodtypes['.$row["ID"].']" value="1">'.$row["TYPE"].'<br>'; } // make the form echo ' <form action="next_page.php" method='post'> '.$list_types.' <input type="submit" value="next"> </form> '; Then, in your next page you should have an array of food types as selected by the user, something like this: foodtypes=array(2,5,6) where each number is the id of the food type as selected by the user. Finally, you would need to modify your sql to only get these types, something like this: $sql_condition=""; if(count($_POST["foodtypes"])>0){ $sql_condition='' AND ("; // loop though items to define sql condition foreach($_POST["foodtypes"] AS $key=>$val{ $sql_condition.='' ID='".$key."' ||"; } // remove final pipes $sql_condition=substr($sql_condition,0,-3); // close condition bracket $sql_condition.=')'; } $result = mysql_query("SELECT * FROM storedata WHERE ID<>0 ".$sql_condition.""); while($row = mysql_fetch_assoc($result)){ ... output results.... { Note - I have not checked this code. I hope this makes sence Chris Quote Link to comment https://forums.phpfreaks.com/topic/173086-form-to-pull-certain-data-from-mysql-db/#findComment-913101 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.