1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using a Form to Search Site. Why Doesn't My Form Work?

Discussion in 'PHP' started by Masterful, Sep 8, 2008.

  1. #1
    OK, guys. Try not to laugh. This is my first attempt at a form using PHP. :eek: Why doesn't it work? :confused:

    I want people to be able to search for the online stores on my site. The stores are in a MySQL table called Store_Table.

    Form:

    <form action="search-results.php" method="post">
    <input type="text" name="search" />
    <input type="submit" value="Search Stores" />
    </form>
    Code (markup):
    Script on search-results.php:

    <?php
    
    $search = $_POST["search"];
    
    $con = mysql_connect("localhost","root");
    
    if (!$con)
    
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT * from Store_Table WHERE store_name LIKE '%$search%'");
    
    echo $result;
    
    else echo "Sorry! Couldn't find store.";
    
    ?>
    PHP:
     
    Masterful, Sep 8, 2008 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    does it print any errors?
     
    serialCoder, Sep 8, 2008 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Sorry! I should have mentioned that. Here's the error:

    Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\xampp\search-results.php on line 19

    When I remove the ELSE statement, it still doesn't work. I don't get an error; it just says "Resource id #3", no matter what I search for. :confused:
     
    Masterful, Sep 8, 2008 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    this line must be causing the error

    else echo "Sorry! Couldn't find store.";
     
    serialCoder, Sep 8, 2008 IP
  5. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #5
    I realize that, dude. Do you know how I can fix it?
     
    Masterful, Sep 8, 2008 IP
  6. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #6
    first this line

    if you are using xampp and installed everything as default try this

    $con = mysql_connect("localhost","root","") or die('cant connect to server'.mysql_error());

    the tell me if it contains errors
     
    serialCoder, Sep 8, 2008 IP
  7. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Yes, I am using XAMPP, and I did install everything as default. However, when I tried what you said, I got the same error:

    Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\xampp\search-results.php on line 15
     
    Masterful, Sep 8, 2008 IP
  8. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #8
    alright, can you try this code

    <?php

    $search = $_POST["search"];

    $con = mysql_connect("localhost","root","") or die('cant connect: '.mysql_error());
    mysql_select_db("ctyi", $con);


    $sql = "SELECT * from Store_Table WHERE store_name LIKE '%$search%'";
    $result = mysql_query($sql) or die('cant select: '.mysql_error());

    while($row = mysql_fetch_assoc($result) )
    {
    print_r($row);
    }


    ?>
     
    serialCoder, Sep 8, 2008 IP
    Masterful likes this.
  9. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #9
    Didn't work, dude. This is what it returned:

    Array ( [advertiser_id] => 1 [advertiser_name] => AbeBooks.com [advertiser_logo] => abebooks.png [advertiser_url] => abebooks-coupons.php )

    Note that, in fact, the table's name is Advertiser_Table, not Store_Table, which is why all the columns' names begin with "Advertiser", too.

    Basically, the code you provided listed the contents of an entire row, depending on which store/advertiser I searched for.

    Thanks for trying, though. I appreciate it. I have added reputation points to your account. :)
     
    Masterful, Sep 8, 2008 IP
  10. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #10
    hmm, but that query is what you have in the code, i basically just modified it, the other parts like formatting, you do on your own i guess
     
    serialCoder, Sep 8, 2008 IP
  11. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #11
    Does my problem have something to do with the fact that there is no IF statement at the beginning?

    But then, it wouldn't make sense for me to have an IF statement.
     
    Masterful, Sep 8, 2008 IP
  12. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #12
    The above one would work for you

    but why you are putting a if else loop for a database connection?
     
    Freewebspace, Sep 8, 2008 IP
    Masterful likes this.
  13. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #13
    Thanks for the help, Freewebspace! However, it still didn't work! Whatever I search for, it just keeps echoing: "Sorry! Couldn't find store."


    I really don't know what I'm doing. :eek: All I want is for people to be able to search for stores on my site, all of which are in a MySQL table. My form seems to work, but not my action script.
     
    Masterful, Sep 8, 2008 IP
  14. forall

    forall Well-Known Member

    Messages:
    176
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #14
    It should work. Besides, if it display message "Sorry! Couldn't find store." thats doesn't mean that it doesn't work, just there are no such as store with that name in mysql database table.
    
    <?php
    $search = $_POST["search"];
    if($search<>''){
    $con = mysql_connect("localhost","root");
    if (!$con){die('Could not connect: ' . mysql_error());}
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT * FROM Store_Table WHERE store_name LIKE '%$search%'");
    while ( $row = mysql_fetch_array(  $result ) )
    	{
    	$store_name = $row['store_name'];
    
    	echo $store_name;
    	}else{echo"Sorry! Couldn't find store.";}
    }else{echo "Search field can't be empy.";}
    ?>
    
    PHP:
     
    forall, Sep 9, 2008 IP
    Masterful likes this.
  15. human

    human Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    <?php
    
    $search = $_POST["search"];
    
    $con = mysql_connect("localhost","root");
    
    if (!$con)
    
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT * from Store_Table WHERE store_name LIKE '%$search%'");
    $num=mysql_num_rows($result);
    if($num>0){
    echo $result;
    }
    else{ echo "Sorry! Couldn't find store.";}
    
    ?>
    PHP:
     
    human, Sep 9, 2008 IP
    Masterful likes this.
  16. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #16
    Didn't work:

    Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\xampp\search-results.php on line 20

    Thank you for the effort. :)
     
    Masterful, Sep 9, 2008 IP
  17. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #17
    This one keeps echoing "Resource id #3" no matter what I search for.

    Nonetheless, thank you for the effort.

    Reputation points added to all of your accounts. :)
     
    Masterful, Sep 9, 2008 IP
  18. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #18
    it keeps echoing "Resource id #3" because that's what the current code does

    $num=mysql_num_rows($result);
    if($num>0){
    echo $result;
    }
    else{ echo "Sorry! Couldn't find store.";}

    if you need professional help, i'll gladly lend a hand :)
     
    serialCoder, Sep 9, 2008 IP
  19. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #19
    OK. I've found a code which retrieves the data that I want. All I need is for someone to please tell me how to incorporate an else statement. If a search query doesn't match any advertiser_names, I want it to echo, "No matches found!".

    <?php
    
    $search = $_POST["search"];
    
    $con = mysql_connect("localhost","root","") or die('cant connect: '.mysql_error());
    
    mysql_select_db("ctyi", $con);
    
    $sql = "SELECT advertiser_name from Advertiser_Table WHERE advertiser_name LIKE '%$search%'";
    
    $result = mysql_query($sql) or die('cant select: '.mysql_error());
    
    while($row = mysql_fetch_assoc($result) )
    
    {
    echo $row['advertiser_name'];
    }
    
    ?> 
    PHP:
     
    Masterful, Sep 9, 2008 IP
  20. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #20
    <?php

    $search = $_POST["search"];

    $con = mysql_connect("localhost","root","") or die('cant connect: '.mysql_error());

    mysql_select_db("ctyi", $con);

    $sql = "SELECT advertiser_name from Advertiser_Table WHERE advertiser_name LIKE '%$search%'";

    $result = mysql_query($sql) or die('cant select: '.mysql_error());
    $numRows = mysql_num_rows($result)

    if( $numRows > 0 )
    {
    while($row = mysql_fetch_assoc($result) )
    {
    echo $row['advertiser_name'];
    }
    }
    else
    {
    echo "No matches found!";
    }

    ?>
     
    serialCoder, Sep 9, 2008 IP