Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Error while displaying a drop-down list based on input from another drop-down list

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???

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda