• PHP Help
  • a href="cocoa_type_chocolate.php?type_chocolate='dark'">Dark</a>

I have two files index.php and cocoa_type_chocolate.php

I have 1 links is dark chocolate.

I want one query when I press dark with method get or post in the where argument

index.php

<a href="cocoa_type_chocolate.php?type_chocolate=dark">Dark</a>

cocoa_type_chocolate.php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root","P-11fl32fg14", "cocoa"); $query = "SELECT chocolate.id_chocolate,chocolate.name_chocolate,type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,type_chocolate.id_chocolate FROM chocolate INNER JOIN type_chocolate ON chocolate.id_chocolate =type_chocolate.id_type_chocolate WHERE type_chocolate='dark'"; $result = mysqli_query($mysqli, $query); /* numeric array */ while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { printf("%s %s %s %s %s\n", $row["id_chocolate"], $row["name_chocolate"], $row["id_type_chocolate"],$row["type_chocolate"],$row["id_chocolate"]);?> <br> <?php } ?>

    It would be helpful if you would spend some time formatting your code. It's hard to read with all the line breaks gone.

    I would point out that you don't bother to check if your mysql connection succeeds before trying to use it. You have also not bothered to explain what your code does when you run it or what you want it to do.

      I have two files index.php and cocoa_type_chocolate.php

      I have 1 links is dark chocolate.

      I want one query when I press dark with method get or post in the where argument

      index.php

      <a href="cocoa_type_chocolate.php?type_chocolate=dark">Dark</a>

      cocoa_type_chocolate.php

      mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 
      $mysqli = mysqli_connect("localhost", "root","P-11fl32fg14", "cocoa"); 
      $query = 
      "SELECT chocolate.id_chocolate,chocolate.name_chocolate,
      type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,
      type_chocolate.id_chocolate 
      FROM chocolate 
      INNER JOIN 
      type_chocolate
       ON chocolate.id_chocolate =type_chocolate.id_type_chocolate 
      WHERE type_chocolate='dark'";
       
      $result = mysqli_query($mysqli, $query); 
      /* numeric array */ 
      while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
      printf("%s %s %s %s %s\n", $row["id_chocolate"], 
      $row["name_chocolate"], $row["id_type_chocolate"],
      $row["type_chocolate"],$row["id_chocolate"]);?> 
      <br> <?php } ?
      ```>
      
      my code show all the dark chocolates but I want to put get in the condicion where, to show milk and white chocolate
      I have index.php and cocoa_type_chocolate.php files
      
      I have two files index.php and cocoa_type_chocolate.php.
      I have 1 links is dark chocolate.
      I want one query when I press one link. 
      I want to change  the condition where, to change between 3 chocolates milk, dark, and white.
      in the condicion where I should use get.
      but I have problem with the url and where condicion.

        So check the input, and if valid, use it in the query, something like

        if(!empty($_REQUEST['type_chocolate'])) {
            if(!in_array($_REQUEST['type_chocolate'], ['dark', 'milk', 'white'])) {
                // handle invalid type here
            } else {
                $query = "
        SELECT chocolate.id_chocolate,chocolate.name_chocolate,
        type_chocolate.id_type_chocolate, type_chocolate.type_chocolate ,
        type_chocolate.id_chocolate 
        FROM chocolate 
        INNER JOIN 
        type_chocolate
         ON chocolate.id_chocolate =type_chocolate.id_type_chocolate 
        WHERE type_chocolate='{$_REQUEST['type_chocolate'])}'";
                // execute query, etc....
            }
        } else {
            // handle error for type not provided
        }
        

        However, instead of putting the value directly into the query, the "right" way to do it (IMO) would be to use a prepared statement using a place-holder and bound parameter. (If that is gibberish to you, google for something like "php mysqli prepared statements").

        Write a Reply...