Onclick单选按钮运行其各自的mysql查询并将其显示在表中

时间:2013-03-25 03:28:33

标签: php mysql sql filter

我一整天都在搜索这段代码,但没有运气,我在搜索时发现了一段代码,显然它无法正常工作,所以我以自己的方式编辑了它,但我仍然没有运气实际上工作,所以,如果有人帮助我使用这段代码,那我们会很高兴。

我想要的是当我选择一个单选按钮然后点击进入下一页我看到应该根据单选按钮显示我的数据库数据..它就像一个过滤掉我的数据库数据的数据库过滤器..

首先,这是第一页..... FILTERPAGE.HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>FILTER PAGE</title>
</head>

<body>
<form action="search.php" method="post" name="filter_option">
<table width="100%" border="0">
    <tr>
    <td>
    <label>
        <input name="filter_options" type="radio" id="filter_options_0" value="default_filter" checked>
       Default</label>
    </td>
      <td><label>
        <input type="radio" name="filter_options" value="special_id_filter" id="filter_options_1">
        Special ID</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="company_name_filter" id="filter_options_2">
        Company Name</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="brand_name_filter" id="filter_options_3">
        Brand Name</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="model_id_filter" id="filter_options_4">
        Model ID</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="colour_filter" id="filter_options_5">
        Colour</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="size_filter" id="filter_options_6">
        Size</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="frame_type_filter" id="filter_options_7">
        Frame Type</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="frame_for_filter" id="filter_options_8">
        Frame For</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="quantity_filter" id="filter_options_9">
        Quantity</label></td>
      <td><label>
        <input type="radio" name="filter_options" value="price_filter" id="filter_options_10">
        Price</label></td>
    </tr>
</table>
<input name="submit" type="submit">
</form>
</body>
</html>

现在这里是第二个是PHP Stuff .......... SEARCH.PHP

<?php 
 mysql_connect("localhost","root","password") or die(mysql_error());

 mysql_select_db("stock_entry") or die(mysql_error());

$filteroption= $_POST['filter_options'];

//To use different queries while searching

if ($filteroption == 'default_filter')
{
$queres = "SELECT * FROM stock_entry_spectacles ORDER BY 'id'";
}
else if ($filteroption == 'special_id_filter')
{
$queres = "SELECT * FROM stock_entry_spectacles ORDER BY 'special_id'";
}
else if ($filteroption == 'company_name_filter')
{
$queres = "SELECT * FROM stock_entry_spectacles ORDER BY 'company_name'";
}
else if ($filteroption == 'brand_name_filter')
{
$queres = "SELECT * FROM stock_entry_spectacles ORDER BY 'brand_name'";
}
else if ($filteroption == 'model_id_filter')
{
$queres = "SELECT * FROM stock_entry_spectacles ORDER BY 'model_id'";
}

$query = $queres;
$result = mysql_query($query) or die(mysql_error());
 while ($row = mysql_fetch_array($result)){
echo "<table class='data' border='0' cellspacing='2' align='center'>
 <tr>
 <td align='center' class='special_id'><input type='text' name='id' value='".$row['special_id']."'></td>
 <td align='center' class='company_nametd'><input type='text' name='company_name' value='".$row['company_name']."'></td>
  <td align='center' class='brand_namestd'> <input type='text' name='brand_name' value='".$row['brand_name']."'></td>
 <td align='center' class='model_idtd'> <input type='text' name='model_id' value='".$row['model_id']."'></td>
 <td align='center' class='colour_selecttd'> <input type='text' name='colour_select' value='".$row['colour_select']."'></td>
 <td align='center' class='size_selecttd'> <input type='text' name='size_select' value='".$row['size_select']."'></td>
 <td align='center' class='type_selecttd'><input type='text' name='type_select' value='".$row['type_select']."'></td>
 <td align='center' class='for_selecttd'> <input type='text' name='for_select' value='".$row['for_select']."'></td>
 <td align='center' class='quantitytd'> <input type='text' name='quantity' value='".$row['quantity']."'></td>
 <td align='center' class='pricetd'> <input type='text' name='price' value='".$row['price']."'></td>
 <tr>
 </table>";
 }  

?>

最后但并非最不重要的Mysql结构......

数据库名称:stock_entry

stock_entry_spectacles的表结构

CREATE TABLE IF NOT EXISTS `stock_entry_spectacles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `special_id` int(225) DEFAULT NULL,
  `company_name` text COLLATE utf8_unicode_ci,
  `brand_name` text COLLATE utf8_unicode_ci,
  `model_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_polish_ci DEFAULT NULL,
  `colour_select` text COLLATE utf8_unicode_ci,
  `size_select` int(11) DEFAULT NULL,
  `type_select` text COLLATE utf8_unicode_ci,
  `for_select` text COLLATE utf8_unicode_ci,
  `quantity` int(11) DEFAULT NULL,
  `price` int(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

转储表格stock_entry_spectacles

的数据
INSERT INTO `stock_entry_spectacles` (`id`, `special_id`, `company_name`, `brand_name`, `model_id`, `colour_select`, `size_select`, `type_select`, `for_select`, `quantity`, `price`) VALUES
(1, NULL, 'Titan', 'abc', '123', 'GunMetal', 50, 'Metal', 'Male', 2, NULL),
(2, NULL, 'AO Specs', 'def', '456', 'Black', 48, 'Metal', 'Male', 6, 500),
(3, NULL, 'Sinora', 'ghi', '789', 'Blue', 13, 'broad sided', 'Female', 3, 460),
(4, NULL, 'Tommy', 'jkl', '963', 'GunMetal', 40, 'Shell', 'Male', 8, 800),
(5, 14873273, 'Manav', 'mno', '852', 'Black', 50, 'Metal', 'Male', 5, 120);

请有人试着帮助我,因为我需要这个代码用于我的一个项目......

1 个答案:

答案 0 :(得分:0)

替换

mysql_connect("localhost","root","password") or die(mysql_error());

。通过

mysql_connect("localhost","root","") or die(mysql_error());