duffman8 Posted October 14, 2011 Share Posted October 14, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/ Share on other sites More sharing options...
Pikachu2000 Posted October 14, 2011 Share Posted October 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279359 Share on other sites More sharing options...
duffman8 Posted October 14, 2011 Author Share Posted October 14, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279368 Share on other sites More sharing options...
AyKay47 Posted October 14, 2011 Share Posted October 14, 2011 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'])){ Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279371 Share on other sites More sharing options...
duffman8 Posted October 14, 2011 Author Share Posted October 14, 2011 Thanks for the suggestion - but the code and comments still 'print' on the browser. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279377 Share on other sites More sharing options...
AyKay47 Posted October 14, 2011 Share Posted October 14, 2011 only other thing that I can think of is the extension of the page you are using is not .php but .html Edit: or you do not have PHP configured.. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279378 Share on other sites More sharing options...
AyKay47 Posted October 14, 2011 Share Posted October 14, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279380 Share on other sites More sharing options...
Pikachu2000 Posted October 15, 2011 Share Posted October 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1279546 Share on other sites More sharing options...
duffman8 Posted October 18, 2011 Author Share Posted October 18, 2011 How embarrassing! :-[ :-[ My opening PHP tags were <? ?> as opposed to <?PHP ?> . It works great now. Thank you both for the support! Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1280141 Share on other sites More sharing options...
AyKay47 Posted October 18, 2011 Share Posted October 18, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/249130-if-issetpost-not-posting/#findComment-1280156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.