简体   繁体   中英

How can I populate a 3rd column result on intersections of 1st and 2nd columns all from the same table that are selected by the user on dropdown?

I have a column vehicle_name and I would like 2 dropdown lists of my 2 other columns namely, vehicle_type and vehicle_color. When these 2 dropdown values are selected and submitted, I would like their intersection to print out the values from vehicle_name. So far my code only generates a dropdown list for vehicle_type, I would need another dropdown for vehicle_colour. Which on submissions populates the intersected values for the vehicle_name. How can I achieve this?

<!DOCTYPE html>
<html>
<body>
<?php
echo "<br>";
echo "<br>";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";


$db = new mysqli($servername, $username, $password, $dbname);


if (!$db) {
    exit('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}
?>

<br>
<div class="label">Select vehicle type:</div>

<select name="payment_method">
    <option value = "">---Select---</option>
<?php
$queryusers = "SELECT DISTINCT vehicle_type FROM orders";
$db = mysqli_query($db, $queryusers);
while ($d=mysqli_fetch_assoc($db)) {
    echo "<option value='{".$d['vehicle_type']."}'>".$d['vehicle_type']."</option>";
}

?>
</select>


<br>
<div class="label_for_time">Select color:</div>

<select name="vehicle_color">
    <option value = "">---Select---</option>

<?php
$query_for_color = "SELECT DISTINCT vehicle_color FROM orders";
$db = mysqli_query($db, $query_for_date);
while ($a=mysqli_fetch_assoc($db)) {
    echo "<option value='{".$a['vehicle_color']."}'>".$a['vehicle_color']."</option>";
}

?>
</select>
<br>
<br>


<button class="go-btn" type="submit">Go</button>
</body>
</html>

As I don't see any AJAX / client-side code in your above example I assume that this is a pure backend-side filtering you are performing. Your code is currently missing parts of the required elements we would need but let's try to figure this out together:

1. Form around your inputs

Add a <form method="POST" target="path-to-your-script.php"> where "path-to-your-script.php" has to be changed to your PHP file name or rewritten URL path. This has to be around the <select> boxes.

You may also use PHP_SELF to set this automatically, this should work in most cases. I used html_entities($var) to avoid any code injections via manipulated URL.

<form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

2. Check for POST'ed variable 'vehicle_type'

In your form, check if a search for available colors has been performed:

<?php
  $query_for_color = "SELECT DISTINCT vehicle_color FROM orders";

  // check if the form variable 'vehicle_type' is available; if so, filter entries.
  if (isset($_POST['vehicle_type'])) {
    $vType= filter_var($_POST['vehicle_type'], FILTER_SANITIZE_STRING);
    $query_for_colors .= ' WHERE vehicle_type = \''.$vType.'\'';
  } 
  $db = mysqli_query($db, $query_for_date);
  while ($a=mysqli_fetch_assoc($db)) {
      echo "<option value='{".$a['vehicle_color']."}'>".$a['vehicle_color']."</option>";
  }
?>

Edit:

As pointed out by one user in the comment, filter_var($var, FILTER_SANITIZE_STRING) won't be enough to avoid potential SQL injections. This was just a recommendation and was not part of the question at all. If you have to work with user data, do more than using filter_var(), instead use either prepared statements or properly escape the user data. There are many tutorials like this one out there that will guide you to safe queries.

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