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)

    Question My search returns no result !!!

    Hello,

    I don't see what's wrong with the following code.
    I want my users to search my database following 2 criteria: firstname and lastname.
    They can choose the 2 criteria at the same time or just one.
    The problem is that the search returns no result when it should.

    Could you help?
    Thanks!

    Ginie

    PHP Code:

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

    if (!isset($search)):

    ?>

    <html>
    <body>

            <form method="POST" ACTION="<?=$PHP_SELF?>" >          
    Firstname <input type="text" name="firstname1" ><br>
    Lastname <input type="text" name="lastname1" ><br>

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

    </body>

    </html>



    <?php
    else:

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


    // Check for selected criteria 

    if($firstname1 && $lastname1 ){
      
    $condition "firstname = $firstname1 and lastname = $lastname1";
    }

    if(
    $firstname1 && !$lastname1 ){ 
    $condition "firstname = $firstname1


    if(!
    $firstname1 && $lastname1 ){ 
    $condition "lastname = $lastname1


     
    // send request


    $result mysql_query ("SELECT * FROM employee WHERE '$condition' "); 

    if (!
    $result){ echo("<p>Error performing query: " mysql_error() . "</p>");    
    exit();
    }


    ?>
    <html>
    <body>

    <?php

    if (mysql_num_rows($result) == 0) {
    echo(
    "<p> The search returned no result. <br>");
    }

    // Print results 

    while ( $row mysql_fetch_array($result) ) {    
    echo(
    "<p>" $row["firstname"] . " " $row["lastname"]</p>");  }

    ?>

    </body>
    </html>

    <?php
    endif;
    ?>
    Ginie G.

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your where clause is broken.

    it should be something like:
    Code:
    WHERE firstname = '$firstname'
    AND   lastname  = '$lastname'
    not:
    Code:
    WHERE 'firstname = $firstname and lastname = $lastname'
    S

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

    Thank you for your answer.
    OK, I've replaced the code as you suggested:

    PHP Code:
    if($firstname1 && $lastname1 ){
      
    $condition "firstname = '$firstname1' and lastname = '$lastname1'";

    But I get this error message:
    Error performing query: You have an error in your SQL syntax near 'What I've typed'' ' at line 1...

    Can you help?
    Thanks!

    Ginie
    Ginie G.

  4. #4
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is "What I've typed" the actual $firstname (or lastname)? Either way, you should be using addslashes (http://php.net/addslashes)

    S

  5. #5
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    You are using quotes in the search and these must be escaped otherwise MySQL thinks the search term ends after I'.

    So you can either use

    addslashes($searchterm);

    or

    mysql_escape_string($searchterm);

    Then you can pass it to the DB and it should work.

  6. #6
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, I wasn't very clear...

    I meant I get this message:

    Error performing query: You have an error in your SQL syntax near 'MyFirstname' at line 1...

    ...if I've entered 'MyFirstname' in my form....

  7. #7
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post your current "Check for selected criteria" and "send request" blocks.

    S

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

    PHP Code:
    <?php 

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

    if (!isset(
    $search)):

    ?>

    <html>
    <body>

            <form method="POST" ACTION="<?=$PHP_SELF?>" >          
    Firstname <input type="text" name="firstname1" ><br>
    Lastname <input type="text" name="lastname1" ><br>


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

    </body>

    </html>



    <?php
    else:

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

    // Check for selected criteria 

    if($firstname1 && $lastname1 ){
      
    $condition "firstname = '$firstname1' and lastname = '$lastname1'";
    }


    if(
    $firstname1 && !$lastname1 ){ 
    $condition "firstname = '$firstname1'" 


    if(!
    $firstname1 && $lastname1 ){ 
    $condition "lastname = '$lastname1'" 


     
    // send request

    $result mysql_query ("SELECT firstname, lastname FROM employee WHERE '$condition' "); 

    if (!
    $result){ echo("<p>Error performing query: " mysql_error() . "</p>");    
    exit();
    }


    ?>
    <html>
    <body>

    <?php

    if (mysql_num_rows($result) == 0) {
    echo(
    "<p><font face='Verdana' size='2'> No result. <br>");
    }

    // Print results 

    while ( $row mysql_fetch_array($result) ) {    
    echo(
    "<p>" $row["firstname"] . "</p>");  }


    ?>

    </body>
    </html>

    <?php
    endif;
    ?>
    Ginie G.

  9. #9
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's what I thought:

    PHP Code:
    $result mysql_query ("SELECT firstname, lastname FROM employee WHERE $condition "); 
    S

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


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
  •