您好我已经问过这个问题,但不幸的是我没有得到适当的答案。我在MySQL数据库中有两个表,名为Items and 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>
并且results.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文件返回页面的任何值,甚至没有错误消息。你能告诉我这里我做错了什么吗?
答案 0 :(得分: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;
?>
答案 1 :(得分:0)
你的问题是:
$query = 'SELECT type FROM tblItem where id='.$_GET['id'];
当
$_GET['id'] = 'Drama'; // Action, etc.
答案 2 :(得分: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']
。