2

There is a drop-down list on the PHP website that contains names taken from the database, after selecting a name from the list, e.g. "Aprilia", I would like all records to be displayed from the database where mark = Aprilia

I know I should add in the code below just in a SELECT WHERE x = y query but I just don't know how to do it; it should look like this in my opinion:

$result = mysqli_query($conn, "SELECT * FROM motorcycles WHERE mark = X");

And I just can't find this X (X should be the user pick from the dropdown list) How to write it?

Photos: https://i.stack.imgur.com/dLhcf.jpg

<?php
    require_once 'header.php';
  
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "projectinz";
    // Create connection
    //$conn = new mysqli($servername, $username, $password, $dbname);
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
?>

<select name="mark" id="mark">
    <?php
        $query = $conn->query("SELECT mark FROM motocykle");

        while($kategoria = mysqli_fetch_array($query))
        {
            echo '<option>'.$kategoria['mark'].'</option>';
        }
    ?>
</select>

<?php
    $wynik = mysqli_query($conn,"SELECT * FROM motocykle");

    while($row = mysqli_fetch_array($wynik))
    {
        echo "<br>".$row['mark']." ".$row['model']." ".$row['capacity']." ".$row['power']."<br>"; 
    }

    mysqli_close($conn);
?> 
</body>
</html>

EDIT !!!
find1.php

<?php
    require_once 'header.php';

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "projectinz";
    // Create connection
    //$conn = new mysqli($servername, $username, $password, $dbname);
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
?>
<form action="../includes/find.inc.php" method="post">
    <select name="mark" id="mark">
        <?php
            $query = $conn->query("SELECT mark FROM motocykle");

            while ($kategoria = mysqli_fetch_array($query)) {
                echo '<option value="id">'.$kategoria['mark'].'</option>';
            }
        ?>
    </select>
    
    <button type="submit" name="findmoto">Find</button>
</form>

<?php
    $wynik = mysqli_query($conn,"SELECT * FROM motocykle ");

    while ($row = mysqli_fetch_array($wynik)) {
        echo "<br>".$row['mark']." ".$row['model']." ".$row['capacity']." ".$row['power']."<br>"; 
    }
    
    mysqli_close($conn);
?>

</body>
</html>

find.inc.php

<?php

session_start();

if (isset($_POST['findmoto'])) {
    require 'dbh.inc.php';

    $id = $_POST["id"];
    $marka = $_POST["mark"];
    
    echo $id;
    echo $marka;

    $display = "SELECT * FROM motocykle WHERE id='$id';";
    $run = mysqli_query($conn, $display);

    if ($run) {
        echo $display;
    } else {
        echo "not";
    }
}

BUT: https://i.stack.imgur.com/tpt1C.jpg Where is the problem?

Dan
  • 5,140
  • 2
  • 15
  • 30
  • Do you mean how to get the value from submitted form? Assuming that your ` – Dharman Dec 21 '20 at 23:40

1 Answers1

0

<option value="1">Name</option> has attribute value witch is sent to POST when form is submitted. You need to set it with probably ID from table and then after POST you filter by that ID in WHERE part of your SQL.

First change

<select name="mark" id="mark">
  <?php
    $query = $conn->query("SELECT id, mark FROM motocykle");

    while($kategoria = mysqli_fetch_array($query))
    {
    echo '<option value="'.$kategoria['id'].'">'.$kategoria['mark'].'</option>';
    }
  ?>
</select>

Second change

if(isset($_POST['findmoto'])) {
        require 'dbh.inc.php';

        $selectedId = intval($_POST["mark"]);
        echo $selectedId;

        $display = "SELECT * FROM motocykle WHERE id='$selectedId';";
        $run = mysqli_query($conn, $display);

        if($run)
        {
            echo $display;
        }
        else
        {
            echo "not";
        }

    }
  • Read what i wote. He doesn't specify VALUE attribute in OPTION tag and missing everything to submit form and handle form in PHP.... can't write him everything.. – Michal Hudák Dec 21 '20 at 23:48
  • Sorry, maybe I misunderstood at first what you were trying to say. – Dharman Dec 21 '20 at 23:49
  • Guys i don't want to have submit button i would like to just refresh automatically, when user will choose option from dropdown list like "Honda" it should automaticly display all record from database where mark="Honda" –  Dec 21 '20 at 23:59
  • Still you need a form wrap and some JS to handle automatic submit... – Michal Hudák Dec 22 '20 at 00:17
  • @MichalHudák i change this code by add submit button and add new page ("find.inc.php") can you please look once again at this code higher? i edit my question –  Dec 22 '20 at 00:26
  • @MichalHudák someting is wrong on the screen display : 1SELECT * FROM motocykle WHERE id='1'; –  Dec 22 '20 at 00:46
  • @MichaloFair that's what you echo in your code.. so i keep it.. (echo $selectedId) => that's the 1 there... and echo $display => that's SQL generated – Michal Hudák Dec 22 '20 at 01:01
  • @MichalHudák of course ! i understand, but how to display choosen record from database now should it be like: $display = mysqli_query("SELECT * FROM motocykle WHERE id='$selectedId';"); echo $display; ?? Cause it doestn work ehh... –  Dec 22 '20 at 01:08
  • @MichaloFair i suggest you look at documentation: https://www.php.net/manual/en/mysqli.query.php You need to populate results into array or whar ever you need variable $run will containt this object > https://www.php.net/manual/en/class.mysqli-result.php You call fetch_assoc or whatever you want. – Michal Hudák Dec 22 '20 at 01:13