Bootstrap Typeahead无法正确连接到SQL数据库 [英] Bootstrap typeahead isn't properly connecting to SQL database

查看:116
本文介绍了Bootstrap Typeahead无法正确连接到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将使用typeahead的Bootstrap搜索栏连接到装有不同食品的远程SQL数据库.我知道预输入在某种程度上是有效的,因为当我输入'C'时会弹出'Chicago'一词,但是,'Chicago'不在我的SQL数据库中.我一直在跟来自YouTube的本教程.

I'm attempting to connect my Bootstrap search bar, which uses typeahead, to a remote SQL database filled with different food items. I know the typeahead is working, on some level, because when I type in 'C' the word 'Chicago' pops up, however, 'Chicago' is not in my SQL database. I was following along with this tutorial from YouTube.

这是我的网页HTML/PHP代码:

Here is my HTML/PHP code for the web page:

这是我的搜索条形码:

<form class="navbar-search">
    <input type="text" id="search" class="search-query span3 pull-right" placeholder="Search" data-provide="typeahead">
    <div class="icon-search"></div>
</form>

这是我页面页脚的JS代码:

Here is the JS code at the footer of my page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-1.9.0.min.js"><\/script>')</script>

<!-- Bootstrap jQuery plugins compiled and minified -->
<script src="js/bootstrap.min.js"></script>

<!-- Bootstrap JS Code -->
<script>
    $(document).ready(function(){
        $('#search').typeahead({
            source: function(query, process) {
                $.ajax({
                    url: 'database.php'
                    type: 'POST';
                    data: 'query=' + query;
                    dataType: 'JSON';
                    async: true;
                    success: function(data) {
                        process(data);
                    }
                });
            }
        });
    });
</script>

这是应该访问SQL数据库的我的database.php代码:

Here is my database.php code that is supposed to access the SQL database:

<?php

if (isset($_POST['query'])) {
    // Connect to database
    mysql_connect('localhost','root','root');
    mysql_select_db('Menu_Items');

    // Retrieve the query
    $query = $_POST['query'];

    // Search the database for all similar items
    $sql = mysql_query("SELECT * FROM foods WHERE name LIKE '%{$query}%'");
    $array = array();

    while ($row = mysql_fetch_assoc($sql)) {
        $array[] = row['name'];
    }

    // Return the json array
    echo json_encode($array);

}

?>

数据库信息如下:

数据库名称:Menu_Items

Database name: Menu_Items

表名称:食品

该表包括2列,第一列是唯一的ID,第二列的标题为名称".

The table consists of 2 columns, the 1st is a unique id, and the 2nd titled "name".

我似乎无法弄清楚我的错在哪里,据我所知我已经正确地遵循了本教程.关于我要搞砸的地方有什么想法吗?

I can't seem to figure out where I'm going wrong with this and as far as I can tell I've followed the tutorial properly. Any ideas on where I'm messing up?

推荐答案

神圣的SQL注入蝙蝠侠!以后请使用这种形式来构成查询:

Holy Sql Injection batman! Please use this form of composing your queries in the future:

$query = sprintf("SELECT firstname, lastname, address, age FROM friends 
    WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

这篇关于Bootstrap Typeahead无法正确连接到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆