0

好的,所以我正在尝试使用 PHP、MySQL 和 AJAX 进行实时搜索。我不确定我是不是错了。我的数据库托管在 phpMyAdmin 上。数据库名称是 Info,我要访问的表是名称。

我的三个页面是 index.php connect.php 和 fetch.php Index.php

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
        <style>

            #here
                {
                    width:400px;
                    height:300px;
                    border: 1px solid grey;
                    display:none;
                }

            #here a{
                display:block;
                width:98%;
                padding:1%;
                font-size:20px;
                border-bottom:1px solid grey;
            }

                </style>
        <body>

            <script src=jq.js></script>

            <script src="jq.js">
                $(document).ready(function(e)
                {
                    $("search").keyup(function()
                    {
                        $("#here").show();
                        var x = $(this).val();
                        $.ajax(
                            {
                                type:'GET',
                                url:'fetch.php',
                                data: 'q='+x,
                                success:function(data)
                                {
                                    $("#here").html(data);
                                }
                                ,
                            });
                    });
                });




            </script>


            <h1>Live Search</h1>
            <input type="search" name="search" id="search">
            <div id="here">

            </div>
        </body>
</html>

获取.php

<?php
if(!empty($_GET['q']))
{

    include 'connect.php';
    $q=$_GET['q'];
    $query = "select * from names where names like '%$q%';";
    while($output=mysqli_fetch_assoc($result))
    {
        echo '<a>'.$output['names'].'</a>';
    }
     $query = "select * from names";
}

获取.php

    ?>

<?php
$host="localhost";
$user="andremac96";
$password="";
$db="Info";
$conn = mysqli_connect($host,$user,$password,$db);
?>
4

1 回答 1

0

这里有一些问题。

首先,您从未执行过以下查询:

$query = "select * from names where names like '%$q%';";

因此,您需要包含mysqli_query()数据库连接并将其传递给它。

$query = mysqli_query($conn, "select * from names where names like '%$q%';");

然后,您将使用错误的$result变量

while($output=mysqli_fetch_assoc($result))

应该是$query,但又是;也查询它。

同样的事情$query = "select * from names";

$query = mysqli_query($conn, "select * from names");

^ 不确定你想用那个做什么。

然后,您忘记将#搜索 id 放入 其中,$("search").keyup(function()其中应显示为:

$("#search").keyup(function()

检查 PHP 和 MySQL 上的错误:

另外,检查你的 JS 控制台。

然后有echo '<a>'.$output['names'].'</a>';

^ 也不确定你想在这里做什么。

然后第二个<script src="jq.js">应该读为<script>.


您当前的代码对SQL 注入是开放的。使用预准备语句,或PDO和预准备语句


HTML贴纸

放在<style>...</style>里面<head></head>。某些浏览器会在 HTML 源代码中抛出警告。


编辑:(重写我使用的测试成功的内容)。

旁注:我添加了<form></form>标签,但没有它们确实可以工作。

文件#1:

<!DOCTYPE html>
<html>
    <head>
        <title></title>

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>

        <style>

            #here
                {
                    width:400px;
                    height:300px;
                    border: 1px solid grey;
                    display:none;
                }

            #here a{
                display:block;
                width:98%;
                padding:1%;
                font-size:20px;
                border-bottom:1px solid grey;
            }

                </style>

    </head>

        <body>


            <script>
                $(document).ready(function(e)
                {
                    $("#search").keyup(function()
                    {
                        $("#here").show();
                        var x = $(this).val();
                        $.ajax(
                            {
                                type:'GET',
                                url:'fetch.php',
                                data: 'q='+x,
                                success:function(data)
                                {
                                    $("#here").html(data);
                                }
                                ,
                            });
                    });
                });


            </script>


<h1>Live Search</h1>
<form>
    <input type="search" name="search" id="search">
</form>

    <div id="here">

    </div>


        </body>
</html>

文件 #2: (fetch.php)

<?php 

 include 'connect.php'; // being the MySQLi_ API

if(!empty($_GET['q']))
{


$q= mysqli_real_escape_string($conn, $_GET['q']);


    $query = mysqli_query($conn, "select * from names where names like '%$q%';") 
             or die(mysqli_error($conn));


    while($output=mysqli_fetch_assoc($query))
    {

        echo '<a>'.$output['names'].'</a>';


    }
//     $query = "select * from names";
}
  • 确保您的.php文件路径正确并且您的脚本可以访问它们。

在文件顶部添加错误报告,这将有助于发现错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:显示错误应该只在暂存中完成,而不是在生产中。

于 2016-04-15T21:16:20.317 回答