Jump to content

if (isset($POST[''])) not posting


duffman8

Recommended Posts

I have created a search facility for a small asset database that is currently hosted locally on my PC using PHP5.3.8, Apache 2.2.2.1 and MySQL 5.0.8.

 

I have two .php pages - search_computers.php and search_computers_sql.php

 

The problem I have is that when I search for e.g. Dell on search_computers.php the web browser opens search_computers_sql.php and displays all of the code and comments on that page from "echo "<p>You forgot to enter" onwards. I have posted the code on both pages below and attached a screenshot of what search_computers_sql.php looks like after submitting a search parameter.

 

This has completely baffled me! I have other forms on this database that have posted and inserted into the MySQL database fine. I've tested the SQL on search_computers_sql.php and that definitely works (I tested it on MySQL query browser).

 

If I was too hazard a guess I would say something isn't quite right with if (isset($_POST['search'])) on search_computers_sql.php but I can't figure it out. Could it possibly be an Apache configuration problem as well?

 

Any help would be much appreciated!

 

search_computers.php

 

				
<h2>Search for a Computer</h2>
<p>Please enter a search parameter to find a computer<u><b></b></u></p>
<br>

<form name="search" method="post" action="./search_computers_result.php">
Seach for: <input type="text" name="find" /> under 
<Select NAME="field">
<Option VALUE="pc_id">PC ID</option>
<Option VALUE="manufacturer">Manufacturer</option>
<Option VALUE="ram">RAM</option>
<Option VALUE="specification">Specification</option>
<Option VALUE="network_point">Network Point</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>	

 

search_computers_sql.php

 

//This is only displayed if they have submitted the form 

if (isset($_POST['search']))
    
$find  = $_POST['find'];
$field = $_POST['field'];
{
	 //If they did not enter a search term we give them an error 
	 if ($find == "") 
	 { 
	 echo "<p>You forgot to enter a search parameter. Please click "?><a href="./search_computers_result.php">here</a></li><?   echo" to try again."; 
	 exit; 
	 } 

	 // We preform a bit of filtering 
	 $find = strtoupper($find); 
	 $find = strip_tags($find); 
	 $find = trim ($find);

	//Connect to local db
	$data_db = mysqli_connect('localhost','root');

	$data = "SELECT c.pc_id, c. manufacturer, c.ram, c.specification, n.network_point, s.status, l.location FROM asset_db.computers c, asset_db.location l, asset_db.network_point n, asset_db.status s WHERE l.id = c.location AND n.id = c.network_point AND s.id = c.status AND upper($field) LIKE '%$find%'";
	$result = mysqli_query($data_db,$data);

		 if (!$result)
                {  echo 'There is no data to display';  exit;  }

		 //And we remind them what they searched for 
			echo "<b>You Searched For:</b> " .$find;
			echo "<br>";
			echo "<br>";
		//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
		$anymatches = mysqli_num_rows($result);
		if ($anymatches > 0)
			{	
			echo ('	</table>
					<table>
					<tr>
					<th style="width: 75px">PC ID</th>
					<th style="width: 125px">Manufacturer</th>
					<th style="width: 75px">RAM</th>
					<th style="width: 175px">Specification</th>
					<th style="width: 85px">Network Point</th>
					<th style="width: 75px">Status</th>
					<th style="width: 175px">Location</th>
					</tr>');

			echo "Please click "?><a href="./search.php">here</a></li><? echo" to enter a new search parameter.<br><br>";				

				 //And we display the results 
				 while($row=mysqli_fetch_array($result,MYSQLI_BOTH)) 
				 {
					$pc_id			= $row['pc_id'];
					$manufacturer   = $row['manufacturer'];
					$ram   			= $row['ram'];
					$specification  = $row['specification'];
					$network_point  = $row['network_point'];
					$status         = $row['status'];
					$location       = $row['location'];
					echo "<tr>";
					echo "<td>$pc_id</td>";
					echo "<td>$manufacturer</td>";
					echo "<td>$ram</td>";
					echo "<td>$specification</td>";
					echo "<td>$network_point</td>";
					echo "<td>$status</td>";
					echo "<td>$location</td>";
					echo "</tr>";
				 }
			echo '	</table>';
			}// End of $anymatches	
	 if ($anymatches == 0) 
	 { 
	 echo "Sorry, but we cannot find a match for your search. Please click "?><a href="./search_computers.php">here</a></li><? echo" to try again.<br><br>"; 
	 } 

}//End of if (isset($_POST['search']))

 

[attachment deleted by admin]

Link to comment
Share on other sites

You have an php closing ?> tag in this line that evidently shouldn't be there.

 

echo "<p>You forgot to enter a search parameter. Please click "?><a href="./search_computers_result.php">here</a></li><?   echo" to try again.";

 

EDIT: Missed the following opening tag, but change it (and all other open tags) to the full <?php tag syntax. Short <? open tag syntax is not enabled by default, and is likely to be removed entirely in a future version of php.

Link to comment
Share on other sites

Thank you for the reply. I've cleaned up those lines of code but it still displays all the code and comments on search_computers_result.php under the browser. (My original post for I mistakenly referred this file as search_computers_sql.php - Sorry!)

 

I've attached another screenshot.

 

[attachment deleted by admin]

Link to comment
Share on other sites

see here in this line..

 

if (isset($_POST['search']))

 

you have not included a starting curly bracket to wrap your code in if the submit button has been pressed.. add the opening curly bracket..

 

if (isset($_POST['search'])){

Link to comment
Share on other sites

where are your opening and closing PHP tags for this.. I don't see them in the provided code.. also, as Pikachu stated.. make sure that the short_open_tag directive in your php.ini file is set to On before trying to use them in your code..

Link to comment
Share on other sites

where are your opening and closing PHP tags for this.. I don't see them in the provided code.. also, as Pikachu stated.. make sure that the short_open_tag directive in your php.ini file is set to On before trying to use them in your code..

 

That isn't what I said at all. I would never recommend that instead of changing the tags so the code remains usable, and retains portability.

Link to comment
Share on other sites

sorry pikachu I didn't see your edit.. to enable short tags you will need to enable the relevant php.ini directive that i listed above.. however we will never suggest that you use short tags instead of full tags.. as this is considered a bad practice and most likely short tags will be removed in future variations of PHP..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.