0

嗨,我已经问过这个问题,但不幸的是我没有得到适当的答案。我在 MySQL 数据库中有两个名为 Items 和 Item 的表,如下所示:

在此处输入图像描述

我有两个 .php 文件作为 bwlow;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>

结果.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;
?>

好吧,第一部分(index.php)是根据 tblItems 表填充列表,但单击列表不会从 results.php 文件返回任何值到页面,甚至没有错误消息。你能告诉我我在这里做错了什么吗?

4

3 回答 3

1

这对你来说会更容易:试试这个,我编辑你的 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>

好的,这是 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;
?>
于 2012-11-14T03:35:05.477 回答
0

你的问题是:

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

什么时候

$_GET['id'] = 'Drama'; // Action, etc.
于 2012-11-14T03:00:59.857 回答
0

在 results.php 中尝试以下查询

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

$row['type']改为$row['Name'].

于 2012-11-14T03:22:05.063 回答