-2

I'm trying to get my search bar to link to a specific page when a user enters something I have this.. Keep in mind this isn't my whole code but it's giving me this error...

Notice: Undefined index: link in D:\xampp\htdocs\wd1_vtec_0100348514\pages\search.php on line 63

<?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'); ?> 
Sarah
  • 7
  • 1
  • 6
  • What is in your `$results`? – Benjamin Diele Jul 23 '14 at 07:00
  • @BenjaminDiele I am wanting it to link to a page but for some reason it's not letting me do so – Sarah Jul 23 '14 at 07:01
  • Do some debugging: use `var_dump($results); die()` immediately before your echo to see what it actually contains – Mark Baker Jul 23 '14 at 07:02
  • @Sarah It's not letting you because there is no `link` in your `$results` array. – Benjamin Diele Jul 23 '14 at 07:03
  • @BenjaminDiele I've looked in my DB and found a column that I want to link it to which has a name of '1' but now it's showing me a 404 – Sarah Jul 23 '14 at 07:04
  • please check your code. its have syntax error. and there is no corresponding if condition of last else condition. please check – karan Jul 23 '14 at 07:05
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Jul 28 '14 at 00:41

1 Answers1

0

You should simply check if there is column link in your database. You show us one error what means that other columns (title and text) exist

You shouldn't also use mysql functions any more. They are deprecated. You should use mysqli or PDO

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291