简体   繁体   English

我该如何解决这个mysql搜索查询

[英]How can I solve this mysql search query

I'm working on a chained select drop down that pulls data from the database to populate the category combobox while the subcategory combo box populates when an option from the category combobox is selected. 我正在处理一个链式选择下拉列表,它从数据库中提取数据以填充类别组合框,同时当选择类别组合框中的选项时,子类别组合框将填充。 The category dropdown box pulls data from database without any issue but I am having problems with getting the subcategorycombo box select data from database based on the 'id' of the option selected in the category combo box.Any help pls?below is my php code and the jquery code that display the data 类别下拉框从数据库中提取数据没有任何问题但是我遇到了使用子类组件框根据类别组合框中选择的选项的“id”从数据库中选择数据的问题。下面的任何帮助请参阅下面的我的PHP代码以及显示数据的jquery代码

 <?php 
    include ('../storescripts/connection.php');

      function ShowCategory(){
    $sql = "SELECT * FROM category";
   $res = mysql_query($sql) or die(mysql_error());
   $category = '<option value="0">Choose...</option>';
     while($row = mysql_fetch_array($res))
     {
         $category .= '<option value = "'.$row['category_id'].'"> '.$row['category_name']. ' </option>';
         }
         return $category;
   }

   function ShowSubCategory(){
         if (!isset($_POST['id'])) 
        {
        //If not isset -> set with dumy value 
        $_POST['id'] = ""; 

            } 

       $sql1 = "SELECT * FROM subcategory WHERE category_id = '$_POST[id]'";    
       $res1 = mysql_query($sql1) or die(mysql_error());   
       $subcategory = '<option value="0"> Choose...</option>';
       while($row = mysql_fetch_array($res1)){

             $subcategory .= '<option value="'.$row['subcategory_id'].'"> '.$row['subcategory_name'].' </option>';

           }
           return $subcategory;
       }

?>

//jquery code // jquery代码

 <script type="text/javascript">
 $(document).ready(function() {

 $("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
 $.post("select_subcat.php", {id:id}, function(data){
    $("select#subcategory").html(data);
    });
 });

 });
 </script>

Try to narrow down what could be the problem. 尽量缩小可能存在的问题。 Have PHP print out the SQL statements to the page when you make a selection. 在进行选择时,让PHP将SQL语句打印到页面。

Then you can feed that exact statement into MySQL (through command line or via phpmyadmin) and see if you get the results you want. 然后你可以将这个确切的语句提供给MySQL(通过命令行或通过phpmyadmin),看看你是否得到了你想要的结果。 If you do, then the problem is later down the line. 如果你这样做,那么问题就在后面。 What do you see? 你看到了什么?

I will agree with earlier poster - if the $_POST id is not set, you fill it with ""? 我同意早期的海报 - 如果没有设置$ _POST id,你用“”填充它? That probably won't get any results. 这可能不会得到任何结果。

One last note: at very least use mysql_real_escape_string() before you ever touch any $_POST or $_GET variables. 最后一点:在触摸任何$ _POST或$ _GET变量之前,至少要使用mysql_real_escape_string() You are opening your site up to a number of SQL injection attacks otherwise. 您正在打开您的站点,否则会进行多次SQL注入攻击。

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

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