为什么我的PHP在我的搜索框中无效

时间:2014-07-23 06:58:46

标签: php

当用户输入我拥有的内容时,我试图让我的搜索栏链接到特定页面。请记住,这不是我的整个代码,但是它给了我这个错误...

注意:未定义的索引:第63行的D:\ xampp \ htdocs \ wd1_vtec_0100348514 \ pages \ search.php中的链接

<?php 
//--- Authenticate code begins here ---
session_start();
//checks if the login session is true
if(!isset($_SESSION['username'])){
header("location:index.php");
}
$username = $_SESSION['username'];

// --- Authenticate code ends here ---


 include ('header.php'); ?> 
 <link rel="stylesheet" type="text/css" href="../css/style1.css">
 <?php
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());


    mysql_select_db("wd1_vtec_0100348514") or die(mysql_error());

?>


<html>
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
    $query = $_GET['query']; 


    $min_length = 3;


    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what I'm looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

       if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

    while($results = mysql_fetch_array($raw_results)){
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop



echo "<p><a href='".$results['text']."'><h3>".$results['title']."</h3>".$results['text']."</‌​p>";
    }

}
    else{ // if there is no matching rows do following
    echo "No results";
}

}

else{ // if query length is less than minimum
echo "Minimum length  is ".$min_length;
}

?>
</body>
<a class="btn btn-search" type="button" href="home.php" >Search Again</a>
</html>






       <div style="float:right">  <a class="btn btn-danger logout" href="logout.php" > Logout</a> </div>

        <div id="menu">
    <ul id="nav">
        <li><a href="home.php" target="_self" >Home</a></li>
        <li><a href="session1.php" target="_self" >Sessions</a>
            <ul>
                <li><a href="session1.php" target="_self" >Session 1</a></li>
                <li><a href="session2.php" target="_self" >Session 2</a></li>
                <li><a href="session3.php" target="_self" >Session 3</a></li>
                <li><a href="session4.php" target="_self" >Session 4</a></li>
                <li><a href="session5.php" target="_self" >Session 5</a></li>
                <li><a href="session6.php" target="_self" >Session 6</a></li>
                <li><a href="session7.php" target="_self" >Session 7</a></li>
                <li><a href="session8.php" target="_self" >Session 8</a></li>
                <li><a href="session9.php" target="_self" >Session 9</a></li>
                <li><a href="session10.php" target="_self" >Session 10</a></li>
                <li><a href="session11.php" target="_self" >Session 11</a></li>
                <li><a href="session12.php" target="_self" >Session 12</a></li>
                <li><a href="session13.php" target="_self" >Session 13</a></li>
                <li><a href="session14.php" target="_self" >Session 14</a></li>



            </ul>
            <li><a href="blog.php" target="_self" >Blog</a></li>


    </ul>

    </div>

    <?php include ('footer.php'); ?> 

1 个答案:

答案 0 :(得分:0)

您只需检查数据库中是否有列link。您向我们显示了一个错误,意味着其他列(标题和文本)存在

您不应再使用mysql功能了。它们已被弃用。您应该使用mysqliPDO