HELP - Complex PHP script for you gurus

Discussion in 'PHP' started by freebie, Dec 20, 2007.

  1. #1
    Hi,

    I have a bit of a complex php script. Details are:

    Background
    Have a simple four field form. Fields are:

    Name
    County
    Date Start
    Date To

    I need the visitor to be able type in all of the above, none of the above or any combination and results from a MySQL database are shown which are narrowed down from the visitors choices above.

    For example if they leave all four fields empty then all results will be displayed

    ...or...

    if they choose just the county, all results will be displayed that match county

    ... or ...

    if they choose county and date to, all results will be displayed that match county and entries up until date to field

    etc...


    Problem

    I can't seem to get this to work :confused:

    I have the following:

    if (($_POST['start'] == "")) {
          $start = '1900-01-01' ;
    	  } else {
    	  $start = $_POST['start'] ;
    	  }
    	  
       if (($_POST['to'] == "")) {
          $to = '2050-01-01' ;
    	  } else {
    	  $to = $_POST['to'] ;
    	  }
    	  
    	if (($_POST['name'] == "") && ($_POST['County'] == "")) {
        $result = mysql_query("select * from table WHERE date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else if (($_POST[name] == "")) {
        $result = mysql_query("select * from table WHERE county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else if (($_POST[County] == "")) {
        $result = mysql_query("select * from table WHERE name='$name' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else {
    	
    	$result = mysql_query("select * from table WHERE name='$name' and county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	}
    	
    	$result1 = mysql_num_rows($result);
    Code (markup):
    Not sure if I have over analysed for this but the results do not match what is being chosen.


    Any help greatly appreciated :)
     
    freebie, Dec 20, 2007 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    You can test empty POST data by doing this
    
    if (!$_POST['data'])
    // or
    if (!isSet($_POST['data']))
    
    PHP:
    And you don't need to wrap it around parenthesis, it just looks more confusing than it already is.
    Don't assign new variables if you only going to use it once. How to put arrays in a string?
    The way you specify your arrays, you need to use the '', like $_POST['data'] NOT $_POST[data], I saw you used
    $_POST[County]

    
    $result = mysql_query("select * from table WHERE county='{$_POST['County']}' and date BETWEEN '$start' and '$to' ") or die(mysql_error());
    
    PHP:
    And this is considered a simple script, it is not complex.
     
    Kaizoku, Dec 20, 2007 IP
  3. BungeeBones

    BungeeBones Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I usually attack something like that by adding temp debugging code in it. My methed is to do something like this

    echo '$this var name = ', $var_name;?><BR><?

    You will be able to tell the value, see if it is correct. Work your way down through the script with the error message. When I use it in an "if" statement I'll usually put on before the if and one inside the if. Then I can tell if my variable is being picked up by the if.

    Hope that helps
     
    BungeeBones, Dec 20, 2007 IP
  4. bsprogs

    bsprogs Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Edit: Deleted - nevermind. they already covered it.
     
    bsprogs, Dec 20, 2007 IP
  5. freebie

    freebie Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the reply guys but despite the changes this still appears not to be working. The new code is

    if (!$_POST['start']) {
          $start = '1900-01-01' ;
    	  } else {
    	  $start = $_POST['start'] ;
    	  }
    	  
       if (!$_POST['to']) {
          $to = '2050-01-01' ;
    	  } else {
    	  $to = $_POST['to'] ;
    	  }
    	  
    	if ((!$_POST['name']) && (!$_POST['County'])) {
        $result = mysql_query("select * from announce WHERE date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else if (!$_POST['name']) {
        $result = mysql_query("select * from announce WHERE county='{$_POST['County']}' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else if (!$_POST['County']) {
        $result = mysql_query("select * from announce WHERE name='{$_POST['name']}'' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	} else {
    	
    	$result = mysql_query("select * from announce WHERE name='$name' and county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); 
    	}
    	
    	$result1 = mysql_num_rows($result);
    Code (markup):

    Surely if I submit the form with just the County it should display just entries in the database under that County - it's displaying all records ?
    :confused:
     
    freebie, Dec 20, 2007 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    maybe you should do a more strict test
    
    } else if (!$_POST['name'] && $_POST['County']) {
    
    } else if ($_POST['name'] && !$_POST['County']) {
    
    PHP:
     
    Kaizoku, Dec 20, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    The first method should be avoided, because it'll throw an E_NOTICE if it's not set.

    And the second might throw an E_STRICT error in future versions of PHP, because they don't want to allow the incorrect case of function names. (Probably to speed PHP up). It should be isset() - All lowercase.
     
    nico_swd, Dec 21, 2007 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    The first method should not give errors, I've been using it, and no errors found in log.
    2nd method, I just change the case so it's easier to read, in practice, I don't emphasize my functions.
     
    Kaizoku, Dec 21, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Set error reporting to E_ALL and you'll see the notices.

    And okay. :)
     
    nico_swd, Dec 21, 2007 IP