SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Lincoln NE
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please help guys!!!!!!!any help is appericated (search page)

    I'm designing a search page to search a MySQL table.

    I am able to view all the records when I hit submit(without entering any value for in the fields on the search page), but I'm not getting any results when I enter any value in any of the text fields or if I select something from the drop
    down menu.
    Any suggestions?

    here is my code for my results page.

    <?php

    $dbcnx = mysql_connect();
    mysql_select_db('roula');

    // The basic SELECT statement
    $select = 'SELECT DISTINCT ID, COMPANY, CODE, MDATE';
    $from = ' FROM LBL';
    $where = ' WHERE 1=1';

    $compsearch = $_POST['compsearch'];
    if ($compsearch != '') { // A company is selected
    $where .= " AND COMPANY='$compsearch'";
    }

    $codesearch = $_POST['codesearch'];
    if ($codesearch != '') { // Some code was specified
    $where .= " AND CODE LIKE '$codesearch'";
    }
    $textsearch = $_POST['textsearch'];
    if ($textsearch != '') { // Some search text was specified
    $where .= " AND COMPANY LIKE '%$textserch%'";
    }
    ?>

    <!-- table goes here -->

    <?php
    $query1 = @mysql_query($select . $from . $where);
    if (!$query1) {
    echo('</table>');
    die('<p>Error retrieving results from database!<br />'.
    'Error: ' . mysql_error() . '</p>');
    }

    $i = 0;
    $c = 0;
    while ($results = mysql_fetch_array($query1)) {

    // you can change the colours here
    $rowcolor = ($i++ % 2 == 0) ? '#FFFFFF' : '#F2F2F2';

    $id = $results['ID'];
    $company = $results['COMPANY'];
    $code = $results['CODE'];
    $address = $results['ADDRESS'];
    $date = $results['MDATE'];

    echo "<TR bgcolor='$rowcolor'><TD ALIGN='left'>$company</TD>
    <TD ALIGN='center'>$code</TD>
    <TD ALIGN='left'>$address</TD>
    <TD ALIGN='center'>$date</TD>
    <TD ALIGN='center'><a href='edit.php?id=$id'>EDIT</A></TD>
    <TD ALIGN='center'><a href='delete.php?id=$id'>DELETE</A></TD></TR>";
    }
    ?>

    Any help is greatly appericated. Thanks much
    Last edited by chafikb; May 18, 2003 at 16:43.

  2. #2
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. Take this with a grain of salt 'cause I may be way off base.
    (Mixed metaphors are just one of 'our' many free services.)

    You have this statement:

    $select = @mysql_query('SELECT DISTINCT ID, COMPANY, CODE, MDATE FROM LBL ORDER BY COMPANY');

    Looks to me like this is executing the query rather than creating a string.

    Here it looks like you are using $select as if it were a string.

    $query1 = @mysql_query($select . $from . $where);

    Would make the error understandable.

    Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3 FROM LBL WHERE 1=1' at line 1

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Lincoln NE
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am able to view all the records when I hit submit(without entering any value for in the fields on the search page), but I'm not getting any results when I enter any value in any of the text fields or if I select something from the drop down menu. What do you think is causing this.
    Any suggestions

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $compsearch = $_POST['compsearch'];
    if ($compsearch != '') { // A company is selected
    $where .= " AND COMPANY='$compsearch'";
    }

    $codesearch = $_POST['codesearch'];
    if ($codesearch != '') { // Some code was specified
    $where .= " AND CODE LIKE '$codesearch'";
    }
    $textsearch = $_POST['textsearch'];
    if ($textsearch != '') { // Some search text was specified
    $where .= " AND COMPANY LIKE '%$textserch%'";
    }
    Two things stand out for me...
    1) You have '%textserch%' when it should be '%textsearch%'.
    2) If you submit 'compsearch' and 'textsearch' at the same time, they will have to be identical (or textsearch a subset of compsearch) because you are comparing both against COMPANY.

    John


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
  •