TheStalker Posted July 17, 2008 Share Posted July 17, 2008 Hi ive been following a book to try and create a search page that searchs my database table and displays the results in a table. Im having trouble with making it work i think there maybe a problem with the form im trying to use? all i get is a page in IE showing this with the url http://localhost/test4/%3C?=$PHP_SELF?> would some one be able to point to what might be wrongin my code please? The website declined to show this webpage HTTP 403 Most likely causes: This website requires you to log in. What you can try: Go back to the previous page. More information here is the code im using <html> <h2>Search</h2> <form name="search" method="post" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> <Select NAME="field"> <Option VALUE="fname">surname</option> <Option VALUE="lname">forname</option> <Option VALUE="info">postcode</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <? php //if they have submitted the form if ($searching =="yes") { echo "<h2>Results</h2><p>"; //did not enter a search term we give an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // connect to our Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("randv") or die(mysql_error()); // preform filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search the table and filed $data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); //display the results echo "<table width=90% align=center border=1><tr>"; while($result = mysql_fetch_array( $data )) { $surname = $row["surname"]; $forename = $row["forename"]; $addressLine1 = $row["addressLine1"]; $addressLine2 = $row["addressLine2"]; $postcode = $row["postcode"]; echo "<tr> <td>$surname</td> <td>$forname</td> <td>$addressLine1</td> <td>$addressLine2</td> <td>$postcode</td></tr> <tr></td> </tr></table>"; } //This counts the number or results $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match the search<br><br>"; } //what was searched for echo "<b>Searched For:</b> " .$find; } ?> </html> any ideas would be great thanks. Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/ Share on other sites More sharing options...
Xurion Posted July 17, 2008 Share Posted July 17, 2008 You're assuming the searching field in for form automatically gives you the $searching variable in the script, which is not the case. You'll need to reference the posted data using: if ($_POST['searching'] == "yes"){ Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592357 Share on other sites More sharing options...
HelpMe1985 Posted July 17, 2008 Share Posted July 17, 2008 <html> <h2>Search</h2> <form name="search" method="post" action="<?php=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> <Select NAME="field"> <Option VALUE="fname">surname</option> <Option VALUE="lname">forname</option> <Option VALUE="info">postcode</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php //if they have submitted the form if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; //did not enter a search term we give an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // connect to our Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("randv") or die(mysql_error()); // preform filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search the table and filed $data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); //display the results echo "<table width=90% align=center border=1><tr>"; while($result = mysql_fetch_array( $data )) { $surname = $row["surname"]; $forename = $row["forename"]; $addressLine1 = $row["addressLine1"]; $addressLine2 = $row["addressLine2"]; $postcode = $row["postcode"]; echo "<tr> <td>$surname</td> <td>$forname</td> <td>$addressLine1</td> <td>$addressLine2</td> <td>$postcode</td></tr> <tr></td> </tr></table>"; } //This counts the number or results $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match the search<br><br>"; } //what was searched for echo "<b>Searched For:</b> " .$find; } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592369 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 hi, thanks for the quick reply. Ive changed that but im still having the same problem search2.php <html> <h2>Search</h2> <form name="search" method="post" action="<?php=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> <Select NAME="field"> <Option VALUE="fname">surname</option> <Option VALUE="lname">forname</option> <Option VALUE="info">postcode</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php //if they have submitted the form if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; //did not enter a search term we give an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // connect to our Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("randv") or die(mysql_error()); // preform filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search the table and filed $data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); //display the results echo "<table width=90% align=center border=1><tr>"; while($result = mysql_fetch_array( $data )) { $surname = $row["surname"]; $forename = $row["forename"]; $addressLine1 = $row["addressLine1"]; $addressLine2 = $row["addressLine2"]; $postcode = $row["postcode"]; echo "<tr> <td>$surname</td> <td>$forname</td> <td>$addressLine1</td> <td>$addressLine2</td> <td>$postcode</td></tr> <tr></td> </tr></table>"; } //This counts the number or results $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match the search<br><br>"; } //what was searched for echo "<b>Searched For:</b> " .$find; } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592375 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 ive just tried changing this search2.php <form name="search" method="post" action="<?php=$PHP_SELF?>"> to this <form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> but i get this "Results You forgot to enter a search term" Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592384 Share on other sites More sharing options...
DeanWhitehouse Posted July 17, 2008 Share Posted July 17, 2008 dont echo it, Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592391 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 <form name="search" method="post" action="<?php $_['PHP_SELF']?>"> still get : Results You forgot to enter a search term its like it not seeing that any thing has been entered in the text box Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592393 Share on other sites More sharing options...
DeanWhitehouse Posted July 17, 2008 Share Posted July 17, 2008 it should be this action="<?php $_SERVER['PHP_SELF']?>"> Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592402 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 it should be this action="<?php $_SERVER['PHP_SELF']?>"> ive changed it to this, Still get the same thing happen. Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592415 Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 my suggestion would be just to change action=<? $_SERVER['PHP_SELF'] ?> to action='fileName.php' Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592417 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 my suggestion would be just to change action=<? $_SERVER['PHP_SELF'] ?> to action='fileName.php' dosnt seem to make any diffrence? im thinking it must be some thing within this bit of code but cant work out what <html> <h2>Search</h2> <form name="search" method="post" action="<?php $_SERVER['PHP_SELF']?>"> Seach for: <input type="text" name="find" /> <Select NAME="field"> <Option VALUE="surname">surname</option> <Option VALUE="forename">forename</option> <Option VALUE="postcode">postcode</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php //if they have submitted the form if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; //did not enter a search term we give an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // connect to our Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("randv") or die(mysql_error()); // preform filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search the table and filed $data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592422 Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 <?php //did not enter a search term we give an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } ?> should be <?php //did not enter a search term we give an error if ($_POST['find'] == "") { echo "<p>You forgot to enter a search term"; exit; } ?> Anytime you pass info from a form you need to access it either via $_POST or $_GET depending on the method you use on the form. Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592425 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 that seems to get me past that. Thanks very much just got some problems with displaying the data in the table now, but ill have a play around with that for a bit. Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592436 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 ok well ive changed a fair few things but am still can not get the results to display in the table? i get this message Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp1\www\test4\search2.php on line 54 <html> <h2>Search</h2> <form name="search" method="post" action="<?php $_SERVER['PHP_SELF']?>"> Seach for: <input type="text" name="find" /> <Select NAME="field"> <Option VALUE="Surname">Surname</option> <Option VALUE="Forename">Forename</option> <Option VALUE="Postcode">Postcode</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php //if they have submitted the form if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; //did not enter a search term we give an error if ($_POST['find'] == "") { echo "<p>You forgot to enter a search term"; exit; } // connect to our Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("randv") or die(mysql_error()); // preform filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); // search the table and filed $data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); echo "<table border='1' width = '1200' align='center'> <br> <tr> <th>CustomerID</th> <th>Surname</th> <th>Forename</th> <th>AddressLine1</th> <th>AddressLine2</th> <th>Postcode</th> </tr>"; //And we display the results while($row = mysql_fetch_array($data)) { echo "<tr>"; echo "<td><center>" . $row['CustomerID'] . "</td>"; echo "<td><center>" . $row['Surname'] . "</td>"; echo "<td><center>" . $row['Forename'] . "</td>"; echo "<td><center>" . $row['AddressLine1'] . "</td>"; echo "<td><center>" . $row['AddressLine2'] . "</td>"; echo "<td><center>" . $row['Postcode'] . "</td>"; echo "</tr>"; } //what was searched for echo "<b>Searched For:</b> " .$find; } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592472 Share on other sites More sharing options...
TheStalker Posted July 17, 2008 Author Share Posted July 17, 2008 think ive just worked this out needed to add $find = $_POST['find']; $field = $_POST['field']; Quote Link to comment https://forums.phpfreaks.com/topic/115204-solved-search-mysql-help-please/#findComment-592558 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.