cubluwt Posted January 21, 2014 Share Posted January 21, 2014 Hi, I'm new here. But many posts here that helped me out before. Thanks alot to this forum, but now I need some help from you guys. First of all this is my database, you can copy and paste it to notepad and save as .sql file to import to mysql. -- phpMyAdmin SQL Dump -- version 3.2.4 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Jan 21, 2014 at 01:14 AM -- Server version: 5.1.41 -- PHP Version: 5.3.1 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `db_cobadulu` -- -- -------------------------------------------------------- -- -- Table structure for table `tbl_barang` -- CREATE TABLE IF NOT EXISTS `tbl_barang` ( `id_barang` int(11) NOT NULL AUTO_INCREMENT, `id_kategori` int(11) NOT NULL, `id_kalsifikasi` int(11) NOT NULL, `nama_barang` varchar(128) NOT NULL, PRIMARY KEY (`id_barang`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Dumping data for table `tbl_barang` -- INSERT INTO `tbl_barang` (`id_barang`, `id_kategori`, `id_kalsifikasi`, `nama_barang`) VALUES (1, 2, 3, 'Samsung S4'), (2, 3, 2, 'Vas Bunga'), (3, 4, 1, 'Mug Porselen'); -- -------------------------------------------------------- -- -- Table structure for table `tbl_kategori` -- CREATE TABLE IF NOT EXISTS `tbl_kategori` ( `id_kategori` int(11) NOT NULL AUTO_INCREMENT, `nama_kategori` varchar(128) NOT NULL, PRIMARY KEY (`id_kategori`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; -- -- Dumping data for table `tbl_kategori` -- INSERT INTO `tbl_kategori` (`id_kategori`, `nama_kategori`) VALUES (1, 'Elektronik'), (2, 'Pecah Belah'), (3, 'Gift'), (4, 'Kerajinan'); -- -------------------------------------------------------- -- -- Table structure for table `tbl_klasifikasi` -- CREATE TABLE IF NOT EXISTS `tbl_klasifikasi` ( `id_klasifikasi` int(11) NOT NULL AUTO_INCREMENT, `nama_klasifikasi` varchar(128) NOT NULL, PRIMARY KEY (`id_klasifikasi`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `tbl_klasifikasi` -- INSERT INTO `tbl_klasifikasi` (`id_klasifikasi`, `nama_klasifikasi`) VALUES (1, 'Ringan'), (2, 'Berat'), (3, 'Cukup Berat'), (4, 'Sangat Berat'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; And then this is the script I have: <script languange="Javascript1.2"> function pilih(id){ location.replace("index.php?id="+id); } </script> <?php $host="localhost"; $user="root"; $pass=""; $db="db_cobadulu"; //koneksi $koneksi=mysql_connect($host,$user,$pass); mysql_select_db($db,$koneksi); if($_GET['']!==""){ $id=$_GET['id']; $query=mysql_query("SELECT b.*, nama_barang, p.nama_kategori, kl.nama_klasifikasi FROM tbl_barang AS b LEFT JOIN tbl_kategori AS p ON b.id_kategori=p.id_kategori LEFT JOIN tbl_klasifikasi AS kl ON b.id_klasifikasi=kl.id_klasifikasi WHERE id_barang='$id'"); ?> <table border="1"> <tr><th>No</th><th>Nama Barang</th><th>Kategori</th><th>Klasifikasi</th> <?php //while($row=mysql_fetch_array($query) or die (mysql_error())){ while($row=mysql_fetch_array($query)){ ?> <tr><td><?php echo $c=$c+1;?></td><td><?php echo $row['nama_barang'];?></td><td><?php echo $row['nama_kategori']; ?></td><td><?php echo $row['nama_klasifikasi']; ?></td> </tr> <?php } ?></table><?php } ?> <html> <head><title>Gudang Barang</title></head> <body> <p> </p> <table border="0"> <tr> </tr> <tr> <td width="195" valign="top"> <select name="id" id="id" onChange="pilih(this.value)"> <option value="0" selected="selected">Pilih Kategori</option> <?php $query_limit=mysql_query("select * from tbl_kategori"); while($row=mysql_fetch_array($query_limit)) { ?><option value="<?php echo $row['id_kategori']; ?>"><?php echo $row['nama_kategori']; ?></option><?php } ?> </select> </td> <td width="195" valign="top"> <select name="id" id="id" onChange="pilih(this.value)"> <option value="0" selected="selected">Pilih Klasifikasi</option> <?php $query_limit=mysql_query("select * from tbl_klasifikasi"); while($row=mysql_fetch_array($query_limit)) { ?><option value="<?php echo $row['id_klasifikasi']; ?>"><?php echo $row['nama_klasifikasi']; ?></option><?php } ?> </select> </td> </tr> </table> </body> </html> The problem is, the output is misserable. Yes it did showing the data from two tables. But it's a mess. What I'm saying is, I intend to show/display the data from table "tbl_barang" that connected with two tables "tbl_kategori" and "tbl_klasifikasi" by choosing data from the dropdown list. But not all the data came out, and also when I was putting the "submit" button it doesn't really work at all. I was looking the old posts here, I've tried googling it but no results. Most of the results is "How to displaying data from database to dropdown list", but what I needed is "How to display data from selected item/data from the dropdown list". I need ur help guys...I cannot sleep, it's always in my mind... Thank you so much for the helps. Best regards, Kris Quote Link to comment https://forums.phpfreaks.com/topic/285549-ask-display-data-from-selected-dropdown-list/ Share on other sites More sharing options...
Barand Posted January 21, 2014 Share Posted January 21, 2014 First you only want to run the search query when if(isset($_GET['id']) && $_GET['id']!==""){... Second, your search query fails because of a spelling error in the table definition CREATE TABLE IF NOT EXISTS `tbl_barang` ( `id_barang` int(11) NOT NULL AUTO_INCREMENT, `id_kategori` int(11) NOT NULL, `id_kalsifikasi` int(11) NOT NULL, <---- should be klasifikasi `nama_barang` varchar(128) NOT NULL, PRIMARY KEY (`id_barang`) ) Third and more serious You have two dropdowns one gives the classification id with name = "id" other gives category id with name = "id" So how do you know if category or classification was selected? they need to pass separate names in your $_GET. However, no matter which id is selected you search for "WHERE id_barang='$id' ". You need to search for whichever id was passed in the $_GET (category or classification) Quote Link to comment https://forums.phpfreaks.com/topic/285549-ask-display-data-from-selected-dropdown-list/#findComment-1465992 Share on other sites More sharing options...
cubluwt Posted January 21, 2014 Author Share Posted January 21, 2014 First you only want to run the search query when if(isset($_GET['id']) && $_GET['id']!==""){... Second, your search query fails because of a spelling error in the table definition CREATE TABLE IF NOT EXISTS `tbl_barang` ( `id_barang` int(11) NOT NULL AUTO_INCREMENT, `id_kategori` int(11) NOT NULL, `id_kalsifikasi` int(11) NOT NULL, <---- should be klasifikasi `nama_barang` varchar(128) NOT NULL, PRIMARY KEY (`id_barang`) ) Third and more serious You have two dropdowns one gives the classification id with name = "id" other gives category id with name = "id" So how do you know if category or classification was selected? they need to pass separate names in your $_GET. However, no matter which id is selected you search for "WHERE id_barang='$id' ". You need to search for whichever id was passed in the $_GET (category or classification) Thank you so much...I get it...I get it... Let me try it first... About the id_kalsifikasi yes, I just found it after I wrote this posts and already fix it.... Quote Link to comment https://forums.phpfreaks.com/topic/285549-ask-display-data-from-selected-dropdown-list/#findComment-1466004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.