简体   繁体   English

下拉列表数据使用php和mysql不匹配

[英]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). (1)。 tbl_barang > tbl_barang>

id_barang id_kategori id_klasifikasi nama_barang id_barang id_kategori id_klasifikasi nama_barang

(2). (2)。 tbl_kategori > tbl_kategori>

id_kategori nama_kategori id_kategori nama_kategori

(3). (3)。 tbl_klasifikasi > tbl_klasifikasi>

id_klasifikasi nama_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. 问题是,当我们选择kategori并选择klasifikasi时,数据“ nama_barang”应显示在第三个下拉列表中。 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'. 但这全是混乱,我不知道如何从不同的表中请求两个id并从一个表“ 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'. 简短的问题是,我不知道如何显示tbl_barang>'nama_barang'中与'tbl_kategori'和'tbl_klasifikasi'相关的ID的数据。

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语法:

$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. 这些ID以数字形式存储时,请不要使用引号将它们引起来。

Hope it helps. 希望能帮助到你。 It is not really clear what is you problem. 目前还不清楚你在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM