Jump to content

Recommended Posts

Here is all the code displaying the html then

the php code.

 

 

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

 

 

<?
  $searchtype = addslashes($_POST['searchtype']);
  $searchterm = addslashes($_POST['searchterm']);
  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");

  if (!$db)
  {
     echo "Error: Could not connect to database.  Please try again later.";
     exit;
  }

  mysql_select_db("bookorama");
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  //$query = "select * from books";
  $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>";
}

?>

First

change

  $searchtype = addslashes($_POST['searchtype']);
  $searchterm = addslashes($_POST['searchterm']);

to

  $searchtype = $_POST['searchtype'];
  $searchterm = $_POST['searchterm'];
echo "searchtype = $searchtype<BR>"
echo "searchterm = $searchterm <BR>"

 

and try that

see what the return page echos

First

change

  $searchtype = addslashes($_POST['searchtype']);
  $searchterm = addslashes($_POST['searchterm']);

to

  $searchtype = $_POST['searchtype'];
  $searchterm = $_POST['searchterm'];
echo "searchtype = $searchtype<BR>"
echo "searchterm = $searchterm <BR>"

 

and try that

see what the return page echos

 

searchtype = author

searchterm = author

 

Number of books found: 0

 

Create a record of something where the author was Bob.

 

Use your search form to enter author as the search type and bob as the search term.

 

Do you get a record back?  If so, good. If not, are you sure all files are on the same server as the database and current, etc.  If you get an error please tell us what it is.

Andy B, ok I got that part working thanks to you,

but now I need to populate a Drop Down Menu.

 

I have some code that I wrote that will populate the

Drop Down Menu pulling data from the database

but I am unable to select data from the menu so

that the menu will populate a table.

 

 

// Calling Function For Drop Down Menu
   Populating_DDM();

function Populating_DDM(){
global $num_results,$result;
echo "<select name=searchtype 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

 

OK FINE!!

 

What does

$query = "select * from books";

show ?

 

 

can you post a few records from the database

 

 


  // I am able to post a few records  by using 
  $query = "select * from books";
  
  // but not by using
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";

OK FINE!!

 

What does

$query = "select * from books";

show ?

 

 

can you post a few records from the database

 

 

  
  // I am able to post a few records  by using 
  $query = "select * from books";
  
  // but not by using
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";

 

$query = "select * from books";

/*
This is all the data in my database.

Name    Age  State  Sponsor 
Jordan  45    NC     Phil Jackson
Bryant  30    LA     Phil Jackson 
Shaq    33    MIA    Pat  Riley

*/

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

 

produces this

 

/*

This is all the data in my database.

 

Name    Age  State  Sponsor

Jordan  45    NC    Phil Jackson

Bryant  30    LA    Phil Jackson

Shaq    33    MIA    Pat  Riley

 

*/

 

from

 

$query = "select * from books";

 

 

Well thats amazing

 

erm..

 

try the

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

 

to

 

    echo "<p><strong>".($i+1).". Name: ";
     echo stripslashes($row["Name"]);
     echo "</strong><br>Age: ";
     echo stripslashes($row["Age"]);
     echo "<br>State: ";
     echo stripslashes($row["State"]);
     echo "<br>Sponsor: ";
     echo stripslashes($row["Sponsor"]);
     echo "</p>";

 

and Good Luck

No!!

 

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

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.