Hi, so this is the most basic thing ever but i am having trouble with it. idk if its because of the code or it just can't be done or idk something. I am trying to retrieve from database by clicking on a [input type submit] button but I am getting Fatal Error near my WHERE syntax.

<input type="submit" name="bf" class="btn btn-default filter-button" value="Bowls & Feeders">

<?php
   if(isset($_GET['bf'])){
      $cat = "Bowls & Feeders";

      $query = $conn->prepare("SELECT * FROM table WHERE type = ':type");
      $query->bindParam(':type', $cat, PDO::PARAM_STR);
      $query->execute();
      $result = $query->fetch(PDO::FETCH_ASSOC);              
    }
?>

I have stared at the code for awhile that i might go blind. please help. thank you in advance.

Dani AI

Generated

Two quick, concrete fixes here.

First, the SQL was malformed: the placeholder must not be wrapped in quotes and the query string must be closed correctly. Second, make sure the submit control is inside a form and you check the same superglobal the form uses (GET vs POST). was right to flag the ampersand — in the HTML value attribute use &amp; so the browser submits a proper & character — but you do not need to double-escape (&amp;amp;). is also right: after executing the statement you must fetch and output (or iterate) the rows.

A minimal, correct example (GET method shown) — adjust names to match your DB/vars:

<form method="get" action="">
  <button type="submit" name="category" value="Bowls &amp; Feeders">Bowls &amp; Feeders</button>
</form>

<?php
if (isset($_GET['category'])) {
    $cat = $_GET['category']; // validate/sanitize as needed
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare("SELECT * FROM `products` WHERE `type` = :type");
    $stmt->execute(['type' => $cat]);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($rows) {
        foreach ($rows as $row) {
            echo htmlspecialchars($row['name']); // example output
        }
    } else {
        echo 'No results found.';
    }
}
?>

Troubleshooting tips: enable PDO exceptions (PDO::ERRMODE_EXCEPTION) so you see real errors; use execute(['type'=>$cat]) or bindValue (not required to use bindParam); wrap identifiers with backticks if a column name might conflict (type can be ambiguous); and when debugging, var_dump the $stmt->errorInfo() or echo the query structure (but never echo raw user input into SQL).

Recommended Answers

All 3 Replies

Well, it was a missing apostrophe(?) i that what you call this ' ? but now its just blank.

You dont do anything with the result so of course it is blank.

Use of & inside REQUEST variable is not good idea. I suggest replace to and but if you realy want to use it then use as &amp;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.