0

I have created a html form. In that I have 2 drop down lists. Drop-down list1 retrieves values from sql database table and displays over there. Now I want to display another drop-down list with input from drop-down list 1.

This is my code:

<label for="bname">Select a Building</label>
  <?php
session_start();
include 'db_connection.php';

$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
    $result = mysqli_query($conn, $sql);

echo "<select name='bname'>";
    while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
  }
 echo "</select>";

echo "<label for='rtype'>Select a rtype</label>";
$sql2 = "SELECT rtype FROM room_details WHERE bname='bname'";
$result2 = mysqli_query($conn,$sql2);

echo "<select name='rtype'>";
    while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
 }
 echo "</select>";

Here once bname value From building_details is selected then based on that input I need to display another dropdown list which is the rtype column from room_details table. Can some one help me with this???

1
  • What is your mistake? You have to specify the error, or give us the name of the error, if you can not get the name of the error, use the solution that I will leave you in the answers. I'll try to help. Commented Apr 29, 2017 at 19:48

2 Answers 2

1

This is the only way I found to do it. I hope it's what you're looking for!

<label for="bname">Select a Building</label>
  <?php
session_start();
include 'db_connection.php';

$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
    $result = mysqli_query($conn, $sql);


$bnames = "";
echo "<select name='bname'>";
    while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
$bnames .= $row['bname'] . ", ";
  }

$bnames = substr($bnames, 0, -2);
 echo "</select>";

echo "<label for='rtype'>Select a rtype</label>";

$sql2 = "SELECT rtype FROM room_details WHERE bname IN($bnames)";

$result2 = mysqli_query($conn,$sql2);

echo "<select name='rtype'>";
    while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
 }
 echo "</select>";

Don't forget to escape the $bnames variable with the htmlspecialchars function if needed to avoid sql injections.

Sign up to request clarification or add additional context in comments.

Comments

0

mysqli_connect_errno() - Returns the error code from last connect call

mysqli_connect_error() - Returns a string description of the last connect error

mysqli_errno() - Returns the error code for the most recent function call

mysqli_sqlstate() - Returns the SQLSTATE error from previous MySQL operation

For more help use this link: http://php.net/manual/en/mysqli.error.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.