Page 1 of 1

One really small Search Problem help pls

Posted: Wed May 04, 2005 4:58 pm
by Arsenal Rule
Hey guys, im trying to get my search to work - i'm really close, because i've created a few search pages and they all work, but this one is really getting to me:

Code: Select all

<?

		//May not be needed depending on the connection.

			$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
				mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

		//build up query using each conditional - else statement refers to search field.

			$conditional = "";

			if ($taxref ==""){
			$conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
			}
			
			$conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

				$query = "SELECT tax_id, c_registration_no, start_date, tax_reference_number FROM tax ORDER BY $Order $InOrder";
				$search = mysql_query($query,$conn);

		$NumRows=mysql_num_rows($search);
		?>
There is something wrong with that. What i'm trying to do is get it to show me the tax_reference_number when i type it into the search field, however, it just keeps bringing me up all the table's data repeatedly no matter what i type in. it's really weird. So, i was wondering if any of you knew what the problem was.

Cheers

Search

Posted: Wed May 04, 2005 6:21 pm
by Arsenal Rule
I would like add to the previous post I have created I really hope someone can help me with this

this is the whole search code I have created so far

Code: Select all

<form name="search" action=<?php Print $_SERVER["PHP_SELF"]; ?> method="post">
            <br>
			 <hr width=100%>
            <?php

			$Order = (isset($_POST['Order']))?$_POST['Order']:"tax_id";
			$InOrder = (isset($_POST['InOrder']))?$_POST['InOrder']:"";
			$taxref = (isset($_POST['taxref']))?$_POST['taxref']:"";

			?> 
            <div align="center" class="style14"><strong>You have to choose One means of search either:
			<br>
            </strong></div>
            <table align=center>
                  <th align=center><span class="style7">Search For  Tax Reference Number: </span>
                    <td><input type ="text" name="taxref" value ="<?print $taxref;?>"size="40">
            </table>
            <br>
            
			
			<table align=center>
            	<td> <span class="style13">Make Search Found Ascending Or Descending:</span>            	  
				<select name="InOrder">
               		<option value="ASC"<?if ($InOrder == "ASC"){print ' SELECTED ';}?>>Ascending</option>
               		<option value="DESC"<?if ($InOrder == "DESC"){print ' SELECTED ';}?>>Descending</option>
               	  </select>
           	</table>
			<br>
            <table align=center>
            <th align=center>
              <td><input type="submit" name="submit" value="Search" />
            </table>
			<br>
			
            <hr width=100%>			
            
            <?

		//May not be needed depending on the connection.

			$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
				mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

		//build up query using each conditional - else statement refers to search field.

			$conditional = "";

			if ($taxref ==""){
			$conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
			}
			
			$conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

				$query = "SELECT tax_id, c_registration_no, start_date, tax_reference_number FROM tax ORDER BY $Order $InOrder";
				$search = mysql_query($query,$conn);

		$NumRows=mysql_num_rows($search);
		?>
            <center>
              <b></strong><?print "There are $NumRows found items relate to your search ";?></b>
              <span class="style3"><span class="style3 style5"></span></span>            
            </center>
            <br>
			<table border="0" cellpadding="0" cellspacing="0" align="center">
          <tr height="20">
            <td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Tax ID </strong></span></td>
            <td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Company Registration Number</strong></span></td>
            <td bgcolor="#93B0B4" width="145"><span class="style14 style4"><strong>Tax Start Date (YYYY-MM-DD)</strong></span></td>
			<td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Tax Reference Number</strong></span></td>
			<td bgcolor="#93B0B4" width="125"><span class="style14 style4"><strong>View Client</strong></span></td>
          </tr>
          <tr>
            <td>
            <?php
	if($NumRows>0){

	//get the first row and begin the table.

	$row = mysql_fetch_assoc ($search);

			//reset the result set back to the first row and display the data set.
			mysql_data_seek ($search, 0);
			while ($row = mysql_fetch_row ($search)) {
			
			echo "<tr valign=top>";
			echo "<td bgcolor=#93B0B4 colspan=6></td>";
			echo "</tr>";
			
			print "<TR>\n";
			print "<TD>$row[0]</TD>";
			print "<TD>$row[1]</TD>";
			print "<TD>$row[2]</TD>";
			print "<TD>$row[3]</TD>";
			if($row[3]=="taxrefno"){
			
			?>
			</td>
          </tr>
        </table>
            <?php
			print $row[0];
			?>
            <?php
			print "</TR>";
			
   		 	}
			else{
			
			?>
          </form>
        </blockquote>
    <td><a href ="viewtax2.php?id=
			<?php
			print $row[0];
			?>
			">View</a></td>
    <?php
			print "</tr>";
			}
			}
			print "</table>";
			}
			
?>

but I think the main problem lies between this part of the code

Code: Select all

<?php

			$Order = (isset($_POST['Order']))?$_POST['Order']:"tax_id";
			$InOrder = (isset($_POST['InOrder']))?$_POST['InOrder']:"";
			$taxref = (isset($_POST['taxref']))?$_POST['taxref']:"";

			?> 
            <div align="center" class="style14"><strong>You have to choose One means of search either:
			<br>
            </strong></div>
            <table align=center>
                  <th align=center><span class="style7">Search For  Tax Reference Number: </span>
                    <td><input type ="text" name="taxref" value ="<?print $taxref;?>"size="40">
            </table>
            <br>
            
			
			<table align=center>
            	<td> <span class="style13">Make Search Found Ascending Or Descending:</span>            	  
				<select name="InOrder">
               		<option value="ASC"<?if ($InOrder == "ASC"){print ' SELECTED ';}?>>Ascending</option>
               		<option value="DESC"<?if ($InOrder == "DESC"){print ' SELECTED ';}?>>Descending</option>
               	  </select>
           	</table>
			<br>
            <table align=center>
            <th align=center>
              <td><input type="submit" name="submit" value="Search" />
            </table>
			<br>
			
            <hr width=100%>			
            
            <?

		//May not be needed depending on the connection.

			$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
				mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

		//build up query using each conditional - else statement refers to search field.

			$conditional = "";

			if ($taxref ==""){
			$conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
			}
			
			$conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

				$query = "SELECT tax_id, c_registration_no, start_date, tax_reference_number FROM tax ORDER BY $Order $InOrder";
				$search = mysql_query($query,$conn);

		$NumRows=mysql_num_rows($search);
		?>
basically it is not responding to the "search" button at its just printing out all the infor mation in the "tax" table regardless what is typed in the search field hope someone can help me with this coz it s really frustrating me

basically I have a search field and search button and if a user types into the search field and the search button is clicked if it matches the name it will show the realted information on the table but if no matches it will just say to items found but at the moment it shows all the information from the "tax" table with out the user entering anything and if the uer enters the reference number into the field and it matches the reference number in the database, instead of it just printing the reference number corresponding to the one that is type in it will not update the table it will still show all the data in the tax table instead. This goes the same for when the reference number doesn't match, instead of it saying no items found it continues to show all the data from the tax table. Basically the reference number typed into is not responding the the submit button I don't know why I rellly hope you or anybody can help I have tried everything and I just can seem to find away to solve this problem

Thank you for your time

Hopefully you can help me to sort this problem out thank you again

Takecare

Posted: Thu May 05, 2005 10:19 am
by pickle
You're constructing the where clause, but you're not adding it into your query. Line 16 & 18 of your original post.