Can't do anything with variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

Sorry for this really beginner question (PHP-Newbie here). I bought the book "PHP and MySQL web developpement" (Welling & Thompson) and I'm trying to recreate the exemples and it won't work. I installed PHP 4.3.1 and MySQL 4.0.12.

The easy part. Here's a simple form page (from the book):
SEARCH.HTML
<html>
<head>
  <title>Book-O-Rama Catalog Search</title>
</head>

<body>
  <h1>Book-O-Rama Catalog Search</h1>

  <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>
</body>
</html>

And now the processing page:
RESULTS.PHP
<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>

The problem. Even if I fill both fields, I always get "Notice: Undefined variable: searchtype"

The only way it would work is if I remove all the "if" part and leave the "select * from books" (without the where clause).

I don't get it. Any help would be much appreciated.

Regards

Gilles

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux