SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Why do I get 100% of the records withthis code?

    When I type 288 into the Search box and click on the Part No. radio button, I should get only 1 result. But I am 100%, that is, I'm getting all the rows in the table! Here is the code:

    PHP Code:
    <?php 

    if (isset($_POST['text'])) 
        { 
        if (!
    preg_match('|^[ .0-9a-zA-Z ]+$|' $_POST['text'])) 
        exit(
    "<p class='errormsg1'>Sorry! You used characters that are not permitted in the search box.</strong></p>"); 
        } 

        
    $text $_POST['text']; 
        
    $text strtolower($text); // MAKE LOWERCASE 
        
    $text strip_tags($text); // REMOVE SYMBOLS 
        
    $text trim($text);       // REMOVE ANY EXTRA WHITE SPACE AT END OF LINE 
        
    $text htmlspecialchars($text);
        
    $group1 $_POST['group1']; 

    // Next line: For when search is done on a page not having the radio buttons with the Search box
    $where = ("PartNo LIKE '%$text%' OR ItemName LIKE '%$text%' OR Descr LIKE '%$text%'");  

    if(
    $group1 == 'both'
        { 
        
    $where=("PartNo LIKE '%$text%' OR ItemName LIKE '%$text%' OR Descr LIKE '%$text%'"); 
        } 

    if(
    $group1 == 'partno'
        { 
        
    $where=("PartNo LIKE '%$text%'"); 
        } 

    if(
    $group1 == 'name'
        { 
        
    $where=("ItemName LIKE '%$text%' OR Descr LIKE '%$text%'"); 
        } 

    $plq mysql_query 
    (    "SELECT Photo, PartNo, ItemName, MSRP, Descr, Brand, DateModified
        FROM ReedyProducts
        WHERE 
    $where
        ORDER BY 0+PartNo
        LIMIT 6" 
    ); 

    if (!
    $plq

      exit(
    "<p class='msgbanner1'>ERROR. Please try again another time.</p>"); 


    // ********************* DISPLAY ERROR MESSAGE IF NO RESULTS  *********************

    if (mysql_num_rows($plq) == 0

      exit(
    "<p class='msgbanner1'>Sorry, I cannot find that information. Please check your typing and try again.</p>"); 


    if (
    mysql_num_rows($plq) > 0

      echo(
    "<p class='msgbanner1'>You searched for $text</p>"); 


    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,454
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I susprct it may have something to do with the &#37; wildcards. But maybe not. If you try
    PHP Code:
    .....
        
    $text htmlspecialchars($text); 
        
    $group1 $_POST['group1']; 
    var_dump($text);
    var_dump($group1);
    ..... 
    do you see what you expect to see? i.e. whatever you entered for text and "partno".

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, I still get all the rows of the table. This worked fine before my hdd crashed. I am assembling the code again and am puzzled by the different behavior. "288" only appears in the PartNo column in only one row.

    Thanks!

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,454
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    What did you see for the values of
    var_dump($text);
    var_dump($group1);

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    All of the rows in the table were returned.

    Thanks!

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Bizarre, I figured it out. Here is the search box code:

    PHP Code:
    <!-- ********************* SEARCH FORM ********************* -->
    <div class="searchbox">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><input name="Text" type="text" value="Type Keyword Here">
    <input name="group1" type="radio" checked value="both">Part No. and Name&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="group1" type="radio" value="partno">Part No.&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="group1" type="radio" value="name">Name&nbsp;&nbsp;&nbsp;
    <input name="Submit1" type="submit" value="Submit"></p></form>
    </div> <!-- end DIV for SEARCHBOX -->
    I changed one line to ...

    PHP Code:
    <p><input name="text" type="text" value=""
    ... and now it works. Very strange.

    Seeve H

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,454
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    The script was looking for $_POST['text'] not $_POST['Text']
    so $text = $_POST['text']; would make $text = NULL or undefined or empty
    (the var_dump would show which). In any case,
    $where=("PartNo LIKE '%$text%'");
    would be
    $where=("PartNo LIKE '%%'");
    essentially anything and everything.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    But I specified that the input would be turned into all lower case to deal with that problem:

    PHP Code:
    $text strtolower($text); // MAKE LOWERCASE 
    Thanks!

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables and array key indices are case sensitive in php.

    strtolower() changes the value of a variable. It does not change the name of the variable.

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I thought strtolower() changes the string to lower case. That's what it says in Kevin's Database book.

    PHP manual (http://us3.php.net/strtolower) states:
    "Returns string with all alphabetic characters converted to lowercase. "

    Thanks!

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it does.

  12. #12
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OOOOOH, I see. The variable does not change case, the content (value) does. I get it.

    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
  •