使用Ajax PHP和jQuery从MySQL获取数据的问题
嗨我已经问过这个问题,但不幸的是我没有得到适当的答案。我在MySQL数据库的两个表叫条目为波纹管:使用Ajax PHP和jQuery从MySQL获取数据的问题
我有两个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表,但点击列表不从Result.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" 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;
?>
谢谢Sephoy,我只是复制并粘贴你的代码,并再次在页面上没有得到任何东西! – user1760110
@ user1760110:只复制这两个文件 – sephoy08
感谢Sephoy08我只是测试他们,他们工作fine.just想知道为什么我们不能够在你的例子中使用jQuery文档就绪方法? – user1760110
您的问题是:
$query = 'SELECT type FROM tblItem where id='.$_GET['id'];
时
$_GET['id'] = 'Drama'; // Action, etc.
你的代码是开放的SQL注入攻击btw。 – keyboardSmasher
嗨KeyboardSmasher,感谢您的评论我更新选择子句$ query ='选择名称从tblItem其中id ='。$ _ GET ['id'];但我确实,nt通过$ _GET ['id'] ='Drama'获得您的观点; //动作等,请你让我知道更多发生了什么?谢谢 – user1760110
@ user1760110:KeyboardSmasher意味着,在你的'tblItem'''''字段中是整数而不是varchar,所以你选择的是watever,你没有'id'就是字符串! – sephoy08
尝试在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']
。
嗨air4x,我更新result.php代码为' $ query =“SELECT tblItem。* FROM tblItems INNER JOIN tblItem USING(id)WHERE tblItems.item ='”。$ mysqli-> real_escape_string($ _ GET ['id '])。“'”; 如果($导致= $ mysqli->查询($查询)) { \t如果($ result-> NUM_ROWS> 0) \t { \t \t $ resultStr = '
- '。 。 \t \t而($行= $ result-> FETCH_ASSOC()) \t \t { \t \t $ resultStr = '
- '; \t \t \t $ resultStr。=''; \t \t \t'
'.$row['name'].'
'; \t \t} \t \t $ resultStr。='
你怎么知道什么是从控制台中检查请求返回...?你知道是否有Ajax请求吗?没有显示点击处理程序代码或ajax代码。如果问题是服务器代码或JavaScript相关,将有助于缩小范围。提供尽可能多的细节,你可以 – charlietfl