Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Why is this PHP Code not searching my database and displaying the results?

    Why is this PHP Code not searching my database and displaying the results?
    Code:
    <?php
    
    if(isset($_POST['submit'])){
    if(isset($_GET['searchform'])){
    if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){
    $name=$_POST['name'];
    
    //connect to the database
    // Include database connection settings
          include('config.inc');
    
    //-select the database to use
    $mydb=mysql_select_db("a3763404_posts");
    
    //-query the database table
    $sql="SELECT Title, Post, Date 
    FROM entries 
    WHERE Title LIKE '%" . $Title . "%' 
    OR Post LIKE '%" . $Post . "%' OR Date LIKE '%" . $Date ."%'";
    
    //-run the query against the mysql query function
    $result=mysql_query($sql);
    
    //-count results
    
    $numrows=mysql_num_rows($result);
    
    echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>"; 
    
    //-create while loop and loop through result set
    while($row=mysql_fetch_array($result)){
    
    	$Title =$row['Title'];
    	$Post=$row['[Post'];
    	$Date = $row['Date'];
    	$Index=$row['Index'];
    		
    //-display the result of the array
    
    echo "<ul>\n"; 
    echo "<li>" . "<a href=\"selected.php?Index=$Index\">"  .$Title .  "</a></li>\n";
    echo "</ul>";
    }
    }
    else{
    echo "<p>Please enter a search query</p>";
    }
    }
    }
    ?>
    The HTML for the input form is:

    Code:
    <form  method="post" action="search.php?go"  id="searchform"> 
    	      <input  type="text" name="name"> 
    	      <input  type="submit" name="submit" value="Find it >>"> 
    	    </form>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    to probably need to backstick the column names ` not a single quote '

    PHP Code:

    $sql
    ="SELECT `Title`, `Post`, `Date` 
    FROM entries 
    WHERE `Title` LIKE '%" 
    $Title "%' 
    OR `Post` LIKE '%" 
    $Post "%' OR `Date` LIKE '%" $Date ."%'"
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Might be just me, but if this is the script, where are you setting the variables used in the query?

    PHP Code:
    //-query the database table
    $sql="SELECT Title, Post, Date 
    FROM entries 
    WHERE Title LIKE '%" 
    $Title "%' 
    OR Post LIKE '%" 
    $Post "%' OR Date LIKE '%" $Date ."%'"
    Fair enough that your looking for $Title, $Post and $Date, but I can't seem to find them being filled somewhere...

    Debugging tip: echo your $sql statement before it's queried, and add an or die(mysql_error()) to your mysql_query()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •