SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Search criteria and php

    Hello,

    I don't see what's wrong in my code. Can you help me?
    I want users to search my table following three criteria: firstname, lastname, and event.
    They can search by the 3 criteria all together or just 2 criteria, or just one.
    The following code returns... nothing!
    How come?



    <?php

    include("../common.php");
    include("../db.php");

    if (!isset($Search)):

    ?>



    <html>

    <body >

    <form method="POST" ACTION="<?=$PHP_SELF?>" >
    <input type="text" name="searchfirstname" >
    <input type="text" name="searchlastname" >
    <input type="text" name="searchevent" >

    <input type="submit" value="Search" name="Search"></td>
    </form>

    </body>
    </html>



    <?php

    else:

    // Process signup submission
    dbConnect('database');

    // Check for selected criteria
    $searchFor = "";

    if($searchfirstname && $searchlastname && $searchevent){
    $searchFor = 'firstname = .$searchfirstname. and lastname = .$searchlastname. and event = .$searchevent.';
    }

    if(!$searchfirstname && $searchlastname && $searchevent){
    $searchFor = 'lastname = .$searchlastname. and event = .$searchevent.';
    }

    if($searchfirstname && !$searchlastname && $searchevent){
    $searchFor = 'firstname = .$searchfirstname. and event = .$searchevent.';
    }

    if($searchfirstname && $searchlastname && !$searchevent){
    $searchFor = 'lastname = .$searchlastname. and firstname = .$searchfirstname.';
    }

    if(!$searchfirstname && !$searchlastname && $searchevent){
    $searchFor = 'event = .$searchevent.' ;
    }

    if($searchfirstname && !$searchlastname && !$searchevent){
    $searchFor = 'firstname = .$searchfirstname.' ;
    }

    if(!$searchfirstname && $searchlastname && !$searchevent){
    $searchFor = 'lastname = .$searchlastname.' ;
    }


    // Send request
    $result = mysql_query ("SELECT * FROM table WHERE' .$searchFor' ");
    if (!$result) { echo("<p>Error performing query: " . mysql_error() . "</p>");
    exit(); }


    ?>
    <html>
    <body>

    <?php

    // Print results
    $i = 1;
    echo ('<table border="0" cellpadding="0" cellspacing="0" width="100%">');
    while ( $row = mysql_fetch_array( $result ) )
    {

    echo ('<tr>');
    echo ('<td width="100%" bgcolor="#008000" ><font face="Verdana" size="2" color="ffffff"><b>Firstname:&nbsp;&nbsp;
    '.$row["title"].'</b></font></td>');
    echo ('</tr>');
    echo ('<tr>');
    echo ('<td width="100%" bgcolor="#ffffff" ><font face="Verdana" size="2"><u>Lastname/u></td>');
    echo ('</tr>');
    echo ('<tr>');
    echo ('<td width="100%" bgcolor="#ffffff" ><font face="Verdana" size="2">'.$row["firstname"].'</font></td>');
    echo ('</tr>');
    echo ('<tr>');
    echo ('<td width="100%" bgcolor="#ffffff" ><font face="Verdana" size="2">'.$row["lastname"].'</font></td>');
    echo ('</tr>');
    echo ('<tr>');
    $i++;
    }
    echo "</table>";?>

    </body>
    </html>

    <?php
    endif;
    ?>
    Ginie G.

  2. #2
    SitePoint Addict IsaacV's Avatar
    Join Date
    Jan 2002
    Location
    Saint Petersburg
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's because of your IF/ELSE statements.
    If you have one statement, you don't need braces. But if you have more than one line of code, then you need the braces.
    use an
    if
    {
    ..code..
    }
    else
    {
    ..code..
    }
    format.
    Pants.
    Join the NYFlava Fashions Contest

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Search criteria and php

    PHP Code:
    if($searchfirstname && $searchlastname && $searchevent){
      
    $searchFor 'firstname = .$searchfirstname. and lastname = .$searchlastname. and event = .$searchevent.';

    noticed the line aboved... what's all those periods ('.') for? did you mean to concatenate the variable names?... if so then something like this would work:
    PHP Code:
    if($searchfirstname && $searchlastname && $searchevent){
      
    $searchFor "firstname = ".$searchfirstname." and lastname = ".$searchlastname." and event = ".$searchevent";


  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by IsaacV
    I think it's because of your IF/ELSE statements.
    If you have one statement, you don't need braces. But if you have more than one line of code, then you need the braces.
    use an
    if
    {
    ..code..
    }
    else
    {
    ..code..
    }
    format.
    hey isaac

    i think the if-else construct he used is ok... it's another format... like this:
    PHP Code:
    if (condition):
    // lots of statements
    else:
    // more statements
    endif; 

  5. #5
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again,

    Thank you for your answers.
    OK, I tried this statement:

    PHP Code:
    if($searchfirstname && $searchlastname && $searchevent){
      
    $searchFor "firstname = ".$searchfirstname." and lastname = ".$searchlastname." and event = ".$searchevent";

    But I get a parse error...
    I also tired the following statement, but once again I have a parse error.

    PHP Code:
    if($searchfirstname && $searchlastname && $searchevent){
      
    $searchFor "firstname = '$searchfirstname' and lastname = '$searchlastname' and event = '$searchevent' ";

    Hmmmmmm.... What could I do ?
    Thanks you!
    Ginie G.

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if($searchfirstname && $searchlastname && $searchevent){
      
    $searchFor "firstname = ".$searchfirstname." and lastname = ".$searchlastname." and event = ".$searchevent;

    sheesh i keep making these stoopid mistakes... there's an extra closing quote... i've removed it in the code here...

    sorry bout that

  7. #7
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked!

    Thank you, php champion !
    Ginie G.

  8. #8
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Me again....

    Well... Actually, I still have a problem...
    My search never returns any results....

    PHP Code:
    $result mysql_query ("SELECT * FROM table WHERE '.$searchFor' "); 
    if (!
    $result) {    echo("<p>Error performing query: " mysql_error() . "</p>");    
    exit();  } 
    Is that correct?
    Ginie G.

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Me again....

    PHP Code:
    $result mysql_query ("SELECT * FROM table WHERE '$searchFor' "); 
    if (!
    $result)
    {
        echo(
    "<p>Error performing query: " mysql_error() . "</p>");    
    exit();

    i removed the extra period (".") before $searchFor... and is the name of your table really 'table'?? i don't think you're allowed to name your table "table" cos that's a SQL reserved word...

  10. #10
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank you so much!
    It worked!

    PS: No, my table name is not really table


Bookmarks

Posting Permissions

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