2012-06-21 109 views
2

这是我得到的。我试图让jquery运行一个MySQL查询。jQuery执行PHP MySQL查询

这是我的PHP:

<select name="kingdom" id="kingdom" > 
    <option value="standard">-- Kingdom --</option> 
     <?php 
      $the_d = $_POST['d']; 
      $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '$the_d'"); 
       while($row = mysql_fetch_array($filter, MYSQL_NUM)) 
        { 
         $row['name']; 
         //echo "<option value='$row[0]'>$row[0]</option>"; 
        } 
     ?> 
</select> 

而且我的jQuery:

$('#domain').change(function() { 

    var selectval = $('#domain').val(); 

    $.post("search.php", { 
     d: selectval 
    }, function (data) { 
     $('.result').html(data); 
    }); 
}); 

现在我只想拥有jQuery的吐出的MySQL结果的值。当我有这个工作时,我可以让他们填充选择框。我现在得到的是search.php的html,但与mysql查询无关。

+2

请注意,您的代码第5行 – thomasrutter

+2

一个SQL注入漏洞,你是不是从查询输出值。您只需在提取循环中具有'$ row ['name']'。你在抓取中使用数字键,所以'$ row ['name']'不会被设置。 –

+0

是否可以打印$ _POST ['d']? –

回答

2

你应该实际打印您从数据库中检索的内容。

<select name="kingdom" id="kingdom" > 
       <option value="standard">-- Kingdom --</option> 
       <?php 
       $the_d = mysql_real_escape_string($_POST['d']);//thanks to @Mansfield 
       $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '".$the_d."'"); 
       while($row = mysql_fetch_array($filter, MYSQL_NUM)) 
       { 
        echo "<option value='$row[0]'>$row[0]</option>"; 

       } 

       ?> 
      </select> 


//or you can also use mysql_fetch_array($filter, MYSQL_ASSOC) if you want to print $row['name'] 

更新答案:

<script src="jquery.min.js"></script> 
<input type='text' class="domain" id="domain"/> 
<div class="result" id="result"></div> 
<script> 
$('#domain').change(function() { 

      var selectval = $('#domain').val(); 

      $.post(
       "test.php", 
       { d : selectval }, 
       function(data) { 
        $('.result').html(data);//my bad should be displaying data retrenched 
       } 
       ); 
}); 
</script> 

///test.php contents: 

<?php 
print $the_d = mysql_real_escape_string($_POST['d']);// see if the scripts are working fine until here, if so, then see below. 
//test your mysql result by doing a print_r($row.)  
?> 

发布的print_r($行)的结果;

+0

谢谢你提醒我关于真正的逃脱字符串!我需要做什么来让我的jQuery来填充它?现在,在这些变化之后,它仍然不起作用。 – chrsgrffth

+0

你可以做alert(selectval); ??? –

+0

是的!它提出了选择onchange的价值。我能从这里做什么? – chrsgrffth