需要帮助我的搜索栏php

时间:2017-03-31 03:29:20

标签: php mysql

我一直关注此链接作为我的搜索栏的指南 https://www.youtube.com/watch?v=Wvs6sbLN3NI&t=678s

我是php和sql的初学者,请帮我解决这个问题。 我不知道在sql语法中做什么。 错误是,注意:未定义的变量:第21行的C:\ xampp \ htdocs \ login-system \ search.php中的q 提前谢谢。

<?php

    $con = mysql_connect('localhost','root','');
    $db = mysql_select_db('accounts');

?>
<!DOCTYPE html>
<html>
<head>
        <title> Search Form </title>
        <link rel="stylesheet" type="text/css" href="css/stylesearch.css">

</head>
<body>

    <form action = "search.php" method ="GET" id="searchForm">
        <input type="text" name="q" id="searchBox" placeholder="Search Here" value="" /><input type="submit" id="searchBtn" value ="GO" />
    </form>

    <?php
        $query = mysql_query("SELECT * FROM provider WHERE provider LIKE '%$q%' OR broker LIKE '%$q%' OR deductible LIKE '%$q%' OR deductible_waiver LIKE '%$q%' OR accredited_banks LIKE '%$q%' OR can_not_offer_mortgaged_with LIKE '%$q%' OR casa_eligibility LIKE '%$q%' OR depreciation LIKE '%$q%' OR special_quotes LIKE '%$q%' OR unit_age_limit LIKE '%$q%' OR provider_information LIKE '%$q%' OR provider_branches LIKE '%$q%' OR mmx_exclusive LIKE '%$q%' OR disclaimer LIKE '%$q%' OR accredited_shops LIKE '%$q%' OR roadside_assistance_number LIKE '%$q%' OR roadside_assistance_coverage LIKE '%$q%' OR check_addressee LIKE '%$q%' OR accounts_details LIKE '%$q%' OR cancellation_charge LIKE '%$q%'");
        $num_rows = mysql_num_rows($query);

        while($row = mysql_fetch_array($query))
        {
            $id = $row['id'];
            $prov = $row['provider'];
            $brok = $row['broker'];
            $deduc = $row['deductible'];
            $deduc_waiver = $row['deductible_waiver'];
            $accre_banks = $row['accredited_banks'];
            $mort = $row['can_not_offer_mortgaged_with'];
            $casa_elig = $row['casa_eligibility'];
            $depre = $row['depreciation'];
            $s_quotes = $row['special_quotes'];
            $age = $row['unit_age_limit'];
            $prov_info = $row['provider_information'];
            $prov_branch = $row['provider_branches'];
            $mmx_exclusive = $row['mmx_exclusive'];
            $disclaimer = $row['disclaimer'];
            $accre_shops = $row['accredited_shops'];
            $roadside_assist_no = $row['roadside_assistance_number'];
            $roadside_assist_cov = $row['roadside_assistance_coverage'];
            $check_addressee = $row['check_addressee'];
            $acc_details = $row['accounts_details'];
            $can_charge = $row['cancellation_charge'];

            echo '<h3>' .$prov .'</h3><p>'. $brok.'<br>'. $deduc.'<br>'. $deduc_waiver.'<br>'. $accre_banks.'<br>'. $mort.'<br>'. $casa_elig.'<br>'. $depre.'<br>'. $s_quotes.'<br>'. $age.'<br>'. $prov_info.'<br>'. $prov_branch.'<br>'. $mmx_exclusive.'<br>'. $disclaimer.'<br>'. $accre_shops.'<br>'. $roadside_assist_no.'<br>'. $roadside_assist_cov.'<br>'. $acc_details.'<br>'. $check_addressee.'<br>'. $can_charge.'</p><br />';
        }
    ?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

你并没有真正说明你的问题在这里。只是查看你的代码,你不是在你的mysqli查询中调用你的数据库,它应该是mysqli_query($ db,&#34; SELECT ....&#34;),其中$ db是你的mysqli_connect变量。 php手册中有几个例子:http://php.net/manual/en/mysqli.query.php

正如mickmacusa上面所说,如果你在后端添加&#34;或者死(mysqli_error($ db)&#34;它将为你提供有关你的sql语法的更多信息。< / p>

答案 1 :(得分:0)

  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

1)尝试使用mysqli_*PDO预备语句

2)首先按照我的示例代码

3)从变量$ q开始。你必须将post值分配给$ q变量,如$ q = $ _ GET [&#39; q&#39;];否则你会得到$q undefined error

        //db connection

        global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

    //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

       mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

         $stmt = $conn->prepare("SELECT * FROM provider
                                WHERE provider LIKE %'".$q."'% 
                                OR broker LIKE %'".$q."'% 
                                OR deductible LIKE %'".$q."'%
                                OR deductible_waiver LIKE %'".$q."'%
                                OR accredited_banks LIKE %'".$q."'% 
                                OR can_not_offer_mortgaged_with LIKE %'".$q."'%
                                OR casa_eligibility LIKE %'".$q."'%
                                OR depreciation LIKE %'".$q."'% 
                                OR special_quotes LIKE %'".$q."'%
                                OR unit_age_limit LIKE %'".$q."'% 
                                OR provider_information LIKE %'".$q."'%
                                OR provider_branches LIKE %'".$q."'% 
                                OR mmx_exclusive LIKE %'".$q."'% 
                                OR disclaimer LIKE %'".$q."'% 
                                OR accredited_shops LIKE %'".$q."'% 
                                OR roadside_assistance_number LIKE %'".$q."'% 
                                OR roadside_assistance_coverage LIKE %'".$q."'% 
                                OR check_addressee LIKE %'".$q."'% 
                                OR accounts_details LIKE %'".$q."'% 
                                OR cancellation_charge LIKE %'".$q."'%");

                            $stmt->execute();


                            $get_result =$stmt->get_result();

                            $row_count= $get_result->num_rows;




    ?>

    <!DOCTYPE html>
    <html>
    <head>
            <title> Search Form </title>
            <link rel="stylesheet" type="text/css" href="css/stylesearch.css">

    </head>
    <body>

        <form action = "search.php" method ="GET" id="searchForm">
            <input type="text" name="q" id="searchBox" placeholder="Search Here" value="" /><input type="submit" id="searchBtn" value ="GO" />
        </form>

        <?php
            if($row_count>0)
            {
                while($row = $get_result->fetch_assoc() )
                {
                    $id = $row['id'];
                    $prov = $row['provider'];
                    $brok = $row['broker'];
                    $deduc = $row['deductible'];
                    $deduc_waiver = $row['deductible_waiver'];
                    $accre_banks = $row['accredited_banks'];
                    $mort = $row['can_not_offer_mortgaged_with'];
                    $casa_elig = $row['casa_eligibility'];
                    $depre = $row['depreciation'];
                    $s_quotes = $row['special_quotes'];
                    $age = $row['unit_age_limit'];
                    $prov_info = $row['provider_information'];
                    $prov_branch = $row['provider_branches'];
                    $mmx_exclusive = $row['mmx_exclusive'];
                    $disclaimer = $row['disclaimer'];
                    $accre_shops = $row['accredited_shops'];
                    $roadside_assist_no = $row['roadside_assistance_number'];
                    $roadside_assist_cov = $row['roadside_assistance_coverage'];
                    $check_addressee = $row['check_addressee'];
                    $acc_details = $row['accounts_details'];
                    $can_charge = $row['cancellation_charge'];

                    echo '<h3>' .$prov .'</h3><p>'. $brok.'<br>'. $deduc.'<br>'. $deduc_waiver.'<br>'. $accre_banks.'<br>'. $mort.'<br>'. $casa_elig.'<br>'. $depre.'<br>'. $s_quotes.'<br>'. $age.'<br>'. $prov_info.'<br>'. $prov_branch.'<br>'. $mmx_exclusive.'<br>'. $disclaimer.'<br>'. $accre_shops.'<br>'. $roadside_assist_no.'<br>'. $roadside_assist_cov.'<br>'. $acc_details.'<br>'. $check_addressee.'<br>'. $can_charge.'</p><br />';
                }

            }


            $stmt->close();
            $conn->close();
        ?>

    </body>
    </html>