简体   繁体   中英

Cannot display search results in a new PHP page

I am having trouble in displaying my search results onto another php page.

Below is my php code for my second php page:

if (isset($_GET['bookTitle'])) {
    $bookTitle = $_GET['bookTitle'];
    $sqlSearch = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice FROM nbc_book
    LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID
    LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID LIKE CONCAT('%', bookTitle, '%') 
    WHERE bookTitle = " . $_GET['booktitle'] ;
    echo "string";
    $resultSearch = mysqli_query($conn,$sqlSearch) or die (mysql_error());

    while ($row = mysqli_fetch_assoc($resultSearch)){
        $category = $row['catDesc'];
        $bookTitle = $row['bookTitle'];
        $publisherName = $row['pubName'];
        $bookYear = $row['bookYear'];
        $bookPrice = $row['bookPrice'];
        echo "Result found!";
    }
}else{

    echo "Result not found!";
}

mysqli_free_result($resultSearch);
mysqli_close($conn);

This is the error i am getting:

Result not found!
Notice: Undefined variable: querySearch in /

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /

Can anyone help me to solve my problem. I would be glad if someone can help me solve this problem.

WHERE bookTitle = " . $_GET['booktitle'] ;

you've missed to wrap value with quotes ' :

 WHERE bookTitle = '" . $_GET['booktitle'] . "' " ;

Update

why I am getting the error messages as shown above?

Because you try to close sql connection apart from opening it!

I've moved mysqli_free_result($resultSearch); and mysqli_close($conn); inside brasked:

if (isset($_GET['bookTitle'])) {
  $bookTitle = $_GET['bookTitle'];
  $sqlSearch = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice FROM nbc_book
LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID
LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID LIKE CONCAT('%', bookTitle, '%') 
WHERE bookTitle = " . $_GET['booktitle'] ;
  echo "string";
  $resultSearch = mysqli_query($conn,$sqlSearch) or die (mysql_error());

  while ($row = mysqli_fetch_assoc($resultSearch)){
    $category = $row['catDesc'];
    $bookTitle = $row['bookTitle'];
    $publisherName = $row['pubName'];
    $bookYear = $row['bookYear'];
    $bookPrice = $row['bookPrice'];
    echo "Result found!";

  mysqli_free_result($resultSearch);
  mysqli_close($conn);
   }
}else{ 
   echo "Result not found!";
}

Seems the $_GET['booktitle'] is not provided, but in your code you try to free result and close nothing!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM