Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218740
Share on other sites

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"

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218742
Share on other sites

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' ";

?>

 

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218756
Share on other sites

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218798
Share on other sites

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
?>




 

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218829
Share on other sites

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..

 

 

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218838
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218842
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218850
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218874
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45057-solved-drop-down-menu/#findComment-218893
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.