Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    removing web form fields with no value

    I want to search a database using multiple user inputs from a web form. if the user decides to search the db using only, say, 3 out of 4 fields is there anyway php can work through the submitted data and pick the fields with inputs, or delete the ones without input (ie "") any ideas or links to tutorils will be great
    cheers
    steve

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    name the form elements as an array like this

    Code:
    <input name="search[]" type="text">
    <input name="search[]" type="text">
    <select name="search[]"></select>
    
    ..and so on
    then in your php page use this

    PHP Code:
    <?php
    if (isset($_POST['search'])) {
        
    $count 0;
        for (
    $i 0;$i count($_POST['search']);$i++) {
            if (
    $i != "") {
                if (
    $count 0) {
                    
    $searchstr .= " AND";
                } else {
                    
    $searchstr " WHERE";
                    
    $count++;
                }
                
    searchstr .= " `column` = ".$_POST['search'][$i];
            }
        }
        
    $query "SELECT * FROM `table`";
        if (isset(
    $searchstr)) { $query .= "$searchstr"; }
        
    //
        //run query etc
        //
    }
    Last edited by SeeIT Solutions; 06-28-2005 at 02:40 PM.

  • #3
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    <?php
    if (!isset($_POST['submit']))
    {
        echo 
    '
        <form action="" method="POST">
        field 1: <input type="text" name="fields[1]"><br>
        field 2: <input type="text" name="fields[2]"><br>
        field 3: <input type="text" name="fields[3]"><br>
        field 4: <input type="text" name="fields[4]"><br>
        field 5: <input type="text" name="fields[5]"><br>
        <input type="submit" name="submit" value="submit">
        </form>
        '
    ;
    }
    else
    {
        echo 
    'you filled in the following fields:<br><br>';
        foreach (
    $_POST['fields'] as $key => $value)
        {
            if (
    $value != '') {echo 'field #' $key ' = ' $value '<br>';}
        }
    }
    ?>

  • #4
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm getting a parse error for this line:

    searchstr .= " type = ".$_POST['search'][$i];

    i've changed the'column' for the column name i want to begin the search with, is this right?
    cheers
    steve

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Its not a variable:
    PHP Code:
    $searchstr .= " `column` = " $_POST['search'][$i]; 
    Also, depending on your field types, I believe that post will send empty fields as null. Since null is technically an input, instead of using an isset($_POST[*]) method, I'd recommend using an !empty($_POST[*]) method instead.

  • #6
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've changed that, but still the same error ie:
    Parse error: parse error in C:\Server\Apache2\local-html\search_york.php on line 240

    it still refers to the line below:
    searchstr .= " 'column' = ".$_POST['search'][$i];

    so its got me totally baffled!!
    thanks for all the help, any other ideas would be very much appreciated
    steve

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    use this instead, make sure the quotes around the column name are ` and not '

    PHP Code:
    $searchstr .= " `type` = '" $_POST['search'][$i]."'";//i assume type is the name of your colum 
    Also, remember to change the table name in this line if you haven't

    PHP Code:
    $query "SELECT * FROM `table`"

  • #8
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    thanks for that, that is working to a point now! however it brings up all entries in the db no matter what i input and whether the entry corrosponds to the info inputted or not, the full php code is:

    Code:
    if (!empty($_POST['search'])) 
    	{ 
        	$count = 0; 
        	for ($i = 0;$i < count($_POST['search']);$i++) 
    	{			 
                     if ($i != "") 
    	    { 
                        if ($count > 0) 
    	       { 
                          $searchstr .= " AND"; 
                        } 
    	       else 
    	{ 
                   $searchstr = " WHERE"; 
                   $count++; 
                } 
                $searchstr .= " `type` = '" . $_POST['search'][$i]."'"; 
     
            } 
          } 
        $query = "SELECT * FROM 'location'"; 
        if (isset($searchstr)) { $query .= "$searchstr"; } 
    
    		$query_result=mysql_query( "select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location"); 
    	 		}			  
    				
    	while($row=mysql_fetch_assoc($query_result))
    	     {
        echo "<tr>";
    		echo "<td>".$row['location_id']."</td>";
    		echo "<td>".$row['owner_id']."</td>";
    	  echo "<td>".$row['loc_name']."</td>";
    		echo "<td>".$row['type']."</td>";		
    		echo "<td>".$row['address_1']."</td>";		
    		echo "<td>".$row['address_2']."</td>";
    		echo "<td>".$row['city']."</td>";	
    		echo "<td>".$row['county']."</td>";
    		echo "<td>".$row['post_code']."</td>";
    		echo "</tr>";
    		}
    
      	mysql_free_result ($query_result);
    		mysql_close();
    Once again thanks for everyones tiime on this, it is appreciated!!
    steve

  • #9
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP Code:
    if (!empty($_POST['search'])) 
        { 
            
    $count 0
            for (
    $i 0;$i count($_POST['search']);$i++) 
        {             
                     if (
    $i != ""
            { 
                        if (
    $count 0
               { 
                          
    $searchstr .= " AND"
                        } 
               else 
        { 
                   
    $searchstr " WHERE"
                   
    $count++; 
                } 
                
    $searchstr .= " `type` = '" $_POST['search'][$i]."'"
     
            } 
          } 
        
    $query "SELECT location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code FROM location"
        if (isset(
    $searchstr)) { $query .= "$searchstr"; } 

            
    $query_result=mysql_query($query); 
                 }              
                    
        while(
    $row=mysql_fetch_assoc($query_result))
             {
        echo 
    "<tr>";
            echo 
    "<td>".$row['location_id']."</td>";
            echo 
    "<td>".$row['owner_id']."</td>";
          echo 
    "<td>".$row['loc_name']."</td>";
            echo 
    "<td>".$row['type']."</td>";        
            echo 
    "<td>".$row['address_1']."</td>";        
            echo 
    "<td>".$row['address_2']."</td>";
            echo 
    "<td>".$row['city']."</td>";    
            echo 
    "<td>".$row['county']."</td>";
            echo 
    "<td>".$row['post_code']."</td>";
            echo 
    "</tr>";
            }

          
    mysql_free_result ($query_result);
            
    mysql_close(); 

  • #10
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for that. that has finally got rid of all the errors (hurray!!) it seems to be working fine
    cheers again
    steve


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •