简体   繁体   中英

Dropdown list data not match using php and mysql

First of all thank you guys for helping me for this little problem I have.

Straight to the matters, but first of all I've been looking about this scripts by googling it and of course in this forum.

My Database:

(1). tbl_barang >

id_barang id_kategori id_klasifikasi nama_barang

(2). tbl_kategori >

id_kategori nama_kategori

(3). tbl_klasifikasi >

id_klasifikasi nama_klasifikasi

And here my scripts >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
    function showBarang(katid) {
        document.frm.submit();
    }
</script>
</head>
<body>
<form action="" method="post" name="frm" id="frm">
<table width="500" border="0">

<!--Kategori-->
  <tr>
    <td width="119">Kategori</td>
    <td width="371">
       <select name="id_kategori" id="id_kategori" onChange="showBarang(this.value);">
       <option value="">--Select--</option>
       <?php
        $sql1="select * from tbl_kategori";
       $sql_row1=mysql_query($sql1);
       while($sql_res1=mysql_fetch_assoc($sql_row1))
       {
       ?>
       <option value="<?php echo $sql_res1["id_kategori"]; ?>" <?php if($sql_res1["id_kategori"]==$_REQUEST["id_kategori"]) { echo "Selected"; } ?>><?php echo $sql_res1["nama_kategori"]; ?></option>
        <?php
        }
        ?>
       </select>
       </td>
  </tr>
 <!-- Klasifikasi -->
 <tr>
    <td>Klasifikasi</td>
    <td id="td_company">
       <select name="id_klasifikasi" id="id_klasifikasi" onChange="showBarang(this.value);">
       <option value="">--Select--</option>
       <?php
        $sql1="select * from tbl_klasifikasi";
       $sql_row1=mysql_query($sql1);
       while($sql_res1=mysql_fetch_assoc($sql_row1))
       {
       ?>
       <option value="<?php echo $sql_res1["id_klasifikasi"]; ?>" <?php if($sql_res1["id_klasifikasi"]==$_REQUEST["id_klasifikasi"]) { echo "Selected"; } ?>><?php echo $sql_res1["nama_klasifikasi"]; ?></option>
        <?php
        }
        ?>
    </select>
       </td>
  </tr>
  <tr>
    <td>Nama Barang</td>
    <td id="td_company">
       <select name="id_barang" id="id_barang">

       <option value="">--Select--</option>
       <?php
       $sql="select * from tbl_barang where id_kategori and id_klasifikasi='$_REQUEST[id_kategori] && [id_klasifikasi]'";
       $sql_row=mysql_query($sql);
       while($sql_res=mysql_fetch_assoc($sql_row))
       {
       ?>
       <option value="<?php echo $sql_res["id_barang"]; ?>"><?php echo $sql_res["nama_barang"]; ?></option>
       <?php
       }
       ?>
    </select>
       </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

The problem is, when we choose kategori and choose klasifikasi the data 'nama_barang' should displays on the third dropdown list. But it's all mess, I don't know how to request two id's from the different tables and display it from one table 'tbl_barang'.

The short issue is, I don't know how to display the data from tbl_barang > 'nama_barang' that have related id's from 'tbl_kategori' and 'tbl_klasifikasi'.

Thank you so much if someone here help me, it's been three days and I'm stuck.

Thank you and best regards,

Kris

I think you are trying to do a join between different tables.

Next point is your sql syntax:

$sql="select * from tbl_barang where id_kategori and id_klasifikasi='$_REQUEST[id_kategori] && [id_klasifikasi]'";

Try this one:

$sql="select * from tbl_barang where id_kategori = ".$_REQUEST[id_kategori]." and id_klasifikasi = ".$_REQUEST[id_klasifikasi].";";

When those ids are stored as numbers do not use quotation marks to wrap them.

Hope it helps. It is not really clear what is you problem.

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