Trium918 Posted March 31, 2007 Share Posted March 31, 2007 I need help selecting all the the information from the database where the selected items are chose from the drop down menu. <? $strServer="localhost"; // Server IP Address 'or' Name $strDatabase="mylabser_cms"; // Database Name $strDB=mysql_connect($strServer) or die("Connection to database failed"); $database=mysql_select_db($strDatabase) or die("Database selection Failed"); //$query="Select * FROM people"; $query="Select * FROM people WHERE".searchtype; //the problem is here but I dont know //the syntax echo "<table border=1 width=50% align=center> <tr> <td align=center>Name</td> <td align=center>Age</td> <td align=center>State</td> <td align=center>Sponsor</td> </tr>"; $result = mysql_query($query); $num_results = mysql_num_rows($result); if ($searchtype) { for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop } echo "</tr> </table>"; ?> <form method="post"> Choose Search Type:<br> <select name="searchtype"> <option value="name">Name <option value="age">Age <option value="state">State <option value="sponsor">Sponsor </select> <br /><br /> <input type=submit value="Search"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/ Share on other sites More sharing options...
papaface Posted March 31, 2007 Share Posted March 31, 2007 try: <? $strServer="localhost"; // Server IP Address 'or' Name $strDatabase="mylabser_cms"; // Database Name $strDB=mysql_connect($strServer) or die("Connection to database failed"); $database=mysql_select_db($strDatabase) or die("Database selection Failed"); $searchtype = mysql_real_escape_string($_POST['searchtype']); //$query="Select * FROM people"; $query="Select * FROM people WHERE type='{$searchtype}'"; echo "<table border=1 width=50% align=center> <tr> <td align=center>Name</td> <td align=center>Age</td> <td align=center>State</td> <td align=center>Sponsor</td> </tr>"; $result = mysql_query($query); $num_results = mysql_num_rows($result); if ($searchtype) { for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop } echo "</tr> </table>"; ?> You chould change: $query="Select * FROM people WHERE type='{$searchtype}'"; type, to the column that you want to use the where clause on. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218740 Share on other sites More sharing options...
MadTechie Posted March 31, 2007 Share Posted March 31, 2007 try $searchtype = mysql_real_escape_string($_POST['searchtype']); $query="Select * FROM people WHERE type=".searchtype; D'oh too slow Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218741 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 It donnot work. I'm not getting an error but it will not pull anything from the database. Maybe it has something to do with my form. <form method="post"> Choose Search Type:<br> <select name="searchtype"> <option value="name">Name <option value="age">Age <option value="state">State <option value="sponsor">Sponsor </select> <br /><br /> <input type=submit value="Search"> </form> This works but I trying to get the drop down menu to work. $query="Select * FROM people" Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218742 Share on other sites More sharing options...
Barand Posted March 31, 2007 Share Posted March 31, 2007 It looks to me as though $searchtype contains the column to be searched. But where is the value you are searching for? Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218746 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 Shouldn't the value = "name" attribute contain the value. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218752 Share on other sites More sharing options...
Barand Posted March 31, 2007 Share Posted March 31, 2007 Add another text box to your form Search for <input type='text' name='searchvalue' size='20'> Then <?php $searchtype = mysql_real_escape_string($_POST['searchtype']); $searchvalue = mysql_real_escape_string($_POST['searchvalue']); $query="Select * FROM people WHERE $searchtype = '$searchvalue' "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218756 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 I'm only trying to get the drop down menu to work. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218776 Share on other sites More sharing options...
Barand Posted March 31, 2007 Share Posted March 31, 2007 Getting it to work is one thing. Getting it to do something useful when it does work is another. I apologise for trying to help. It won't happen again. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218782 Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 *slaps barands hands* Your just too helpful you are mate!! *tut tut* Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218784 Share on other sites More sharing options...
Barand Posted March 31, 2007 Share Posted March 31, 2007 There are those you can help and those you can't. As far as I'm concerned, this guy's on his own from now on. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218787 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 You people are rude. I would never give up on no one. I would always help until they understand. I cannont help that I'm not as skillful as you at php. Im trying!! Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218791 Share on other sites More sharing options...
Barand Posted March 31, 2007 Share Posted March 31, 2007 On the contrary, I offered help and you threw it back in my face. And I did apologize. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218796 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 I have this book where it uses the same method you gave me. All I was asking is there away that I can get it woking without adding a extra text field? <html> <head> <title>Book-O-Rama Search Results</title> </head> <body> <h1>Book-O-Rama Search Results</h1> <? if (!$searchtype || !$searchterm) { echo "You have not entered search details. Please go back and try again."; exit; } $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); @ $db = mysql_pconnect("localhost", "bookorama", "bookorama"); if (!$db) { echo "Error: Could not connect to database. Please try again later."; exit; } mysql_select_db("books"); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = mysql_fetch_array($result); echo "<p><strong>".($i+1).". Title: "; echo stripslashes($row["title"]); echo "</strong><br>Author: "; echo stripslashes($row["author"]); echo "<br>ISBN: "; echo stripslashes($row["isbn"]); echo "<br>Price: "; echo stripslashes($row["price"]); echo "</p>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218798 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 The drop down menu is being populated, but only when the first For loop is commented out. How can I fix it where the menu is populated and able to pull data from the database. I need the drop down menu to populate the table inside the first commented For loop. <? $strServer="localhost"; // Server IP Address 'or' Name $strDatabase="mylabser_cms"; // Database Name $strDB=mysql_connect($strServer) or die("Connection to database failed"); $database=mysql_select_db($strDatabase) or die("Database selection Failed"); $query="Select sponsor FROM people"; echo "<table border=1 width=50% align=center> <tr> <td align=center>Name</td> <td align=center>Age</td> <td align=center>State</td> <td align=center>Sponsor</td> </tr>"; $result = mysql_query($query); $num_results = mysql_num_rows($result); /* for ($i = 0; $i < $num_results; $i++) { $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop echo "</tr> </table>";*/ // Calling Function For Drop Down Menu Populating_DDM(); function Populating_DDM(){ global $num_results,$result; echo "<select value=''>Sponsor</option>"; // printing the list box select command for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo "<option value=$row[$i]>$row[sponsor]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box }// End of function ?> Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218829 Share on other sites More sharing options...
MadTechie Posted March 31, 2007 Share Posted March 31, 2007 i have no idea what your trying to do.. did you skip a chapter as $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); isn't going to work.. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218838 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 i have no idea what your trying to do.. did you skip a chapter as $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); isn't going to work.. It is pulling data from the database and populating the tables. I also have this: for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); $name = htmlspecialchars( stripslashes($row["name"])); $age = htmlspecialchars( stripslashes($row["age"])); $state = htmlspecialchars( stripslashes($row["state"])); $sponsor = htmlspecialchars( stripslashes($row["sponsor"])); // Output echo " <tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td>"; }// End of For loop Both are working. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218842 Share on other sites More sharing options...
MadTechie Posted March 31, 2007 Share Posted March 31, 2007 that will work.. i still don't know what the problem is .. or what your goal is.. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218844 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 My goal is to use a Drop Down Menu that is populated by the database. The user is then able to select a name from the Menu that pulls all of the information for a particular person from the database. Finally, a table is populated with the information of the selected person. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218850 Share on other sites More sharing options...
Trium918 Posted March 31, 2007 Author Share Posted March 31, 2007 My goal is to use a Drop Down Menu that is populated by the database. The user is then able to select a name from the Menu that pulls all of the information for a particular person from the database. Finally, a table is populated with the information of the selected person. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218874 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 //Why wouldn't this work? $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; //This will display data $query = "select * from books"; Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218886 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 whats $searchtype $searchterm set to ? Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218888 Share on other sites More sharing options...
AndyB Posted April 1, 2007 Share Posted April 1, 2007 More constructively, no it won't produce select * from books it will produce something exactly like your first query with the variables substituted with ... whatever those are/wherever they came from. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218891 Share on other sites More sharing options...
Trium918 Posted April 1, 2007 Author Share Posted April 1, 2007 whats $searchtype $searchterm set to ? Here it is: <form action="results.php" method="post"> Choose Search Type:<br> select name="searchtype"> <option value="author">Author <option value="title">Title <option value="isbn">ISBN </select> <br> Enter Search Term:<br> <input name="searchterm" type=text> <br> <input type=submit value="Search"> </form> AndyB, That is what I think is wrong. The variables get their value from the form. For some reason it will not display any data even when I enter something in the textbox. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218893 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 ~Sighs~ maybe you need to post all your code as that didn't help.. and maybe a DB schemer. Quote Link to comment https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218894 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.