Retreiving all data from mySQL database!

Discussion in 'PHP' started by npsari, Apr 7, 2007.

  1. #1
    I have a comboBox called Countries
    I want if a user selects 'All countries' he gets all results from the database

    I used this code, and it worked...

    mysql_select_db(database);
    
    $Countries = $_POST['Coutries'];
    $q = "SELECT * from table";
    if ($Countries!="All countries") {
        $q.= " WHERE Countries = '$Countries'";
    }
    
    $q.= " ORDER by Date DESC LIMIT 100";}
    Code (markup):
    But I have a second combobox which is Town
    I want if the user selects all towns, he also gets all results
    I did this, but it doesnt work

    mysql_select_db(database);
    
    $Countries = $_POST['Coutries'];
    $q = "SELECT * from table";
    if ($Countries!="All countries") {
        $q.= " WHERE Countries = '$Countries'";
    }
    
    $Town = $_POST['Town'];
    $q = "SELECT * from table";
    if ($Town!="All Towns") {
        $q.= " WHERE Town = '$Town'";
    }
    
    $q.= " ORDER by Date DESC LIMIT 100";
    Code (markup):
    How can I make it work?
     
    npsari, Apr 7, 2007 IP
  2. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The first thing I notice is that both queries query the table named 'table'.

    Is the code you provided the actual code, or have your replaced the actual table names with table ??


    This is really a different matter, but you should not take input directly from POST values to the SQL statement. That allows SQL injection attacks.
    Take a look at mysql-real-escape-string documentation.
     
    kjewat, Apr 7, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    I hate it when people post their problems on multiple forums. Some of the people who try to help will do their work for nothing, 'cause someone may answered it on another forum.

    So everybody, before replying see if the problem has been solved here.

    http://www.programmingtalk.com/showthread.php?p=117629
     
    nico_swd, Apr 7, 2007 IP