Jump to content

Recommended Posts

I only want the one record to come back but it brings back evert single record within the database. Please help, thanks :)

 

Form To Fill In To Send It to the Database:

    <center><h1>SEARCH ANIMALS RECORDS</h1>
    <form action="animals-search.php" method="post">
        <input type="text" name="search_query"><br>
        <br>
        <input type="submit" value="Submit">
        
    </form></center>

PHP:

// Collect the search query from a form input
$search = $_POST['search'];

// Construct an SQL query to search for data in the MySQL database
$sql = "SELECT id, animal_type, animal_breed, colour, owner_name, address, telephone, mobile, email, offence, offence_date, offence_location, case_status, case_ref, action_required, action_taken, microchipped, microchip_number, aggresive, dangerous, lost, date_lost, location_lost, stolen, date_stolen, location_stolen, found, date_found, location_found, other_information * FROM animals WHERE 1";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // Output the search results
    while($row = mysqli_fetch_assoc($result)) {
        echo "<br> " . $row["br"] . "<br>";
        echo "Animal Type: " . $row["animal_type"] . "<br>";
        echo "Animal Breed: " . $row["animal_breed"] . "<br>";
        echo "Colour: " . $row["colour"] . "<br>";
        echo "Owner Name: " . $row["owner_name"] . "<br>";
        echo "Address: " . $row["address"] . "<br>";
        echo "Telephone: " . $row["telephone"] . "<br>";
        echo "Mobile: " . $row["mobile"] . "<br>";
        echo "Email: " . $row["email"] . "<br>";
        echo "Offence: " . $row["offence"] . "<br>";
        echo "Offence Date: " . $row["offence_date"] . "<br>";
        echo "Offence Location: " . $row["offence_location"] . "<br>";
        echo "Case Status: " . $row["case_status"] . "<br>";
        echo "Case Ref: " . $row["case_ref"] . "<br>";
        echo "Action Required: " . $row["action_required"] . "<br>";
        echo "Action Taken: " . $row["action_taken"] . "<br>";
        echo "Microchipped: " . $row["microchipped"] . "<br>";
        echo "Miccrochip Number: " . $row["microchip_number"] . "<br>";
        echo "Aggressive: " . $row["aggressive"] . "<br>";
        echo "Dangerous: " . $row["dangerous"] . "<br>";
        echo "Lost: " . $row["lost"] . "<br>";
        echo "Date Lost: " . $row["date_lost"] . "<br>";
        echo "Location Lost: " . $row["location_lost"] . "<br>";
        echo "Stolen: " . $row["stolen"] . "<br>";
        echo "Date Stolen: " . $row["date_stolen"] . "<br>";
        echo "Location Stolen: " . $row["location_stolen"] . "<br>";
        echo "Found: " . $row["found"] . "<br>";
        echo "Date Found: " . $row["date_found"] . "<br>";
        echo "Location Found: " . $row["location_found"] . "<br>";
        echo "Other Information: " . $row["other_information"] . "<br>";
        echo "<br> " . $row["br"] . "<br>";
        echo "<hr> " . $row["hr"] . "<br><br>";

    }
} else {
    echo "No Results Found";
}

// Close the database connection
mysqli_close($conn);
?>

Edited by AlphaOneIntelligence
28 minutes ago, ginerjm said:

The purpose of the where clause is for you to specify which record you want.

He has. A query selects all records for which the WHERE expression evaluates to "true". As "1" is true for all records, they have specified that all records are selected.

You can do a full text content search on a `content` field, but the main thing is you have to search a column in order to find what you are looking for.

This is done in PDO, but gives an example on what I'm trying to say -

 

 // Extract the search term and heading from the request, if they exist.
    $searchTerm = isset($request['searchTerm']) ? $request['searchTerm'] : null;
    $heading = isset($request['heading']) ? $request['heading'] : null;

    // If a search term was provided, use a full-text search on the 'content' field.
    // Before this can work, you'll need to make sure your content column is indexed for full-text searching.
    // You can do this with the following SQL command:
    // Example:
    // ALTER TABLE gallery ADD FULLTEXT(content);
    if($searchTerm !== null) {
        $sql = "SELECT * FROM gallery WHERE MATCH(content) AGAINST(:searchTerm IN NATURAL LANGUAGE MODE) LIMIT 1";
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':searchTerm', $searchTerm);

        // If a heading was provided, search for exact matches on the 'heading' field.
    } else if($heading !== null) {
        $sql = "SELECT * FROM gallery WHERE heading = :heading LIMIT 1";
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':heading', $heading);

        // If neither a search term nor a heading was provided, throw an exception.
    } else {
        throw new Exception("No valid search term or heading provided");
    }

    // Execute the prepared statement.
    $stmt->execute();

    // Fetch the results and handle them as needed.
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

 

Edited by Strider64

It does appear this is intended to be a search so the where criteria needs to be implemented.  Towards that goal, this is not going to work:

<input type="text" name="search_query"><br>

Followed by:

$search = $_POST['search'];

 

See the issue?  (search_query is not search).

 

This query is also not going to work as written:

....,  date_stolen, location_stolen, found, date_found, location_found, other_information * 

Typically either enumerate the columns or use the wildcard '*', but certainly this syntax of 'other_information *' is invalid, as you would at very least need 'other_information, *'.

 

 

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.