简体   繁体   English

使用Ajax PHP和jQuery从MySQL获取数据的问题

[英]Issue with Getting Data from MySQL using Ajax PHP and jQuery

Hi I already asked this question but unfortunately I didn't get appropriate answer for that. 嗨,我已经问过这个问题,但是不幸的是我没有得到适当的答案。 I have two tables in MySQL database called Items and Item as bellow: 我在MySQL数据库中有两个表,分别称为Items和Item,如下所示:

在此处输入图片说明

I have two .php files as bwlow; 我有两个.bwlow文件。 the index.php and results.php the index.php is like: index.php和results.php index.php类似于:

 <html>
 <head></head>
 <body>
 <?php
   $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
    if ($mysqli->connect_errno) 
     {
      die('Unable to connect!');
     }
    else{
         $query = 'SELECT * FROM tblItems';
         if ($result = $mysqli->query($query)) {
            if ($result->num_rows > 0) {
   ?>  
    <p> Select a Genre
       <ul>
    <?php     
       while($row = $result->fetch_assoc()){
     ?>      
  <li><div class="selectGenre"><?php echo $row['item']; ?></div></li>     
  <?php           
    }
   ?>
        </ul>
</p>
<p id="result"></p>
<?php
}
else 
{
    echo 'No records found!';
}
$result->close();
}
else 
{
echo 'Error in query: $query. '.$mysqli->error;
 }
}
$mysqli->close();
?>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"           type="text/javascript"></script>
 <script type="text/javascript">
    $(document).ready(function()
    {
        $('.selectGenre').click(function()
        {
          if($(this).html() == '') return;
            $.get(
                'results.php',
                { id : $(this).html() },
                function(data)
                {
                    $('#result').html(data);
                }
            );
        });
    });
   </script>
  </body>
  </html>

and the results.php is: 并且result.php是:

<?php
  $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
  $resultStr = '';
   $query = 'SELECT type FROM tblItem where id='.$_GET['id'];
   if ($result = $mysqli->query($query)) 
   {
    if ($result->num_rows > 0) 
   {
    $resultStr.='<ul>';
     while($row = $result->fetch_assoc())
     {
    $resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['type'];
   '</li>';
  }
   $resultStr.= '</ul>';
  }
  else
  {
$resultStr = 'Nothing found';
  }
 }
echo $resultStr;
?>

well, the first part(index.php) is populating the list base on the tblItems table but clicking on the list not returning any values to the page from the results.php file, not even an error message. 好吧,第一部分(index.php)是基于tblItems表填充列表,但是单击列表时不会从results.php文件向页面返回任何值,甚至不会返回错误消息。 Can you please let me know what I am doing wrong here? 您能告诉我我在做什么错吗?

this will be easier for you: Try this one, i edit your index.php code: 这对您来说会更容易:尝试这一步,我编辑您的index.php代码:

 <html>
 <head></head>
 <body>
 <?php
   $mysqli = new mysqli('localhost', 'root', '', 'moviedb');
    if ($mysqli->connect_errno) 
     {
      die('Unable to connect!');
     }
    else{
         $query = 'SELECT * FROM tblItems';
         if ($result = $mysqli->query($query)) {
            if ($result->num_rows > 0) {
   ?>  
    <p> Select a Genre
       <ul>
    <?php     
       while($row = $result->fetch_assoc()){
     ?>      
  <li><div class="selectGenre" onclick="return printSearch('<?php echo $row['id']; ?>');"><?php echo $row['item']; ?></div></li>     
  <?php           
    }
   ?>
        </ul>
</p>
<p id="result"></p>
<?php
}
else 
{
    echo 'No records found!';
}
$result->close();
}
else 
{
echo 'Error in query: $query. '.$mysqli->error;
 }
}
$mysqli->close();
?>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"           type="text/javascript"></script>
 <script type="text/javascript">
    function printSearch(idVal)
    {

            $.get(
                'results.php',
                { id : idVal },
                function(data)
                {
                    $('#result').html(data);
                }
            );
    }
   </script>
  </body>
  </html>

Ok here is for result.php 好的,这里是result.php

<?php

$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
$resultStr = '';
 $query = 'SELECT * FROM tblItem where id='.$_GET['id'];
if ($result = $mysqli->query($query)) 
{
    if ($result->num_rows > 0) 
    {
        $resultStr.='<ul>';
        while($row = $result->fetch_assoc())
        {
            $resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['Name'].
                            '</li>';
        }
        $resultStr.= '</ul>';
    }
    else
    {
        $resultStr = 'Nothing found';
    }
 }
echo $resultStr;
?>

Your problem is: 您的问题是:

$query = 'SELECT type FROM tblItem where id='.$_GET['id'];

when 什么时候

$_GET['id'] = 'Drama'; // Action, etc.

Try the below query in results.php 在results.php中尝试以下查询

$sql  = "SELECT tblItem.* FROM tblItems 
  INNER JOIN tblItem USING(id)
  WHERE tblItems.item = '".$mysqli->real_escape_string($_GET['id'])."'";

Also change $row['type'] to $row['Name'] . 还要将$row['type']更改$row['type'] $row['Name']

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

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