run query with empty form field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geraldjr30
    New Member
    • Jan 2009
    • 60

    run query with empty form field

    hi,

    i have the following:

    Code:
    $T_GROUP = $_POST["T_GROUP"];
    $site = $_POST["site"];
    ....
    $sql="SELECT day, field1, field3, field4, field5 FROM qry_sum WHERE field3 = '".$T_GROUP."' AND field2 LIKE '".$site."' AND day = 'SUNDAY';
    $rs=odbc_exec($conn,$sql);
    if (!$rs)
      {exit("Error in SQL");}
    
    while (odbc_fetch_row($rs))
    {
      $SFT=odbc_result($rs, shift);
      $BER=odbc_result($rs, infocount);
      $TTLS=odbc_result($rs2, ttl);
      $TTLGRAND=odbc_result($rs3, ttl_GRAND);	
    print "<br>";
    
    echo $SFT;
    when there is a value in the "site" form field the query retrieves records related to that site. i would like to know if there is a way the query can retrieve ALL results when the "site" form field is empty?



    thanks in advance,
    geebee
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Sure, you can do that. Just check to see whether your 'site' field is empty (or not set).

    Code:
    <?php
    
    // ...
    
    if ( ! isset ( $_POST['site'] ) || $_POST['site'] == "" )
    {
        // Code for all sites goes here
    }
    else
    {
        // Code for specific site goes here
    }
    
    ?>
    Then, in your SELECT query, simply omit any WHERE clause from the query - that way, when you run the query, it will return every row from the table (ie. all your sites).

    Hope this helps,
    Markus.

    Comment

    • geraldjr30
      New Member
      • Jan 2009
      • 60

      #3
      ok.. i now have...

      Code:
      if ( ! isset ( $_POST['fac'] ) || $_POST['fac'] == "" ) 
      ...
      $sql="SELECT admitday, shift, SRG_GROUP, sum(infocount) as infocount2, hospitalcode FROM qry_sum WHERE SRG_GROUP = '".$SRG_GROUP."' AND hospitalcode LIKE '".$fac."' AND admitday ='".$count."'";
      else
      $sql="SELECT admitday, shift, SRG_GROUP, infocount, hospitalcode FROM qry_sum WHERE SRG_GROUP = '".$SRG_GROUP."' AND admitday ='".$count."'";
      
      ....
      ....
      if ( ! isset ( $_POST['fac'] ) || $_POST['fac'] == "" ) 
      $BER=odbc_result($rs, infocount2);
      else
      $BER=odbc_result($rs, infocount);
      but i am getting an error...

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by geraldjr30
        ok.. i now have...

        Code:
        if ( ! isset ( $_POST['fac'] ) || $_POST['fac'] == "" ) 
        ...
        $sql="SELECT admitday, shift, SRG_GROUP, sum(infocount) as infocount2, hospitalcode FROM qry_sum WHERE SRG_GROUP = '".$SRG_GROUP."' AND hospitalcode LIKE '".$fac."' AND admitday ='".$count."'";
        else
        $sql="SELECT admitday, shift, SRG_GROUP, infocount, hospitalcode FROM qry_sum WHERE SRG_GROUP = '".$SRG_GROUP."' AND admitday ='".$count."'";
        
        ....
        ....
        if ( ! isset ( $_POST['fac'] ) || $_POST['fac'] == "" ) 
        $BER=odbc_result($rs, infocount2);
        else
        $BER=odbc_result($rs, infocount);
        but i am getting an error...
        It would help to know the error.

        Comment

        • geraldjr30
          New Member
          • Jan 2009
          • 60

          #5
          "Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'admitday' as part of an aggregate function., SQL state 37000 in SQLExecDirect "

          Comment

          Working...