Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 Is the filename of that script test2.php (I hope)? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120328 Share on other sites More sharing options...
diamondlifestyle Posted October 8, 2010 Author Share Posted October 8, 2010 yup, so it goes on a loop back to itself, is this where I'm getting the error? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120334 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 No, that's fine. Are you getting anything from the output of the print_r() at the top of the page after you submit the form? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120340 Share on other sites More sharing options...
diamondlifestyle Posted October 8, 2010 Author Share Posted October 8, 2010 sorry for the delay, something very odd has happened, I've loaded the page again to check your last question and it's now saying there's an unexpected } on line 162. I've looked at all the curly brackets and can't see a mistake, I've deleted them all individually and tried it, it still doesn't work, I've deleted the whole script and pasted it from the one you wrote, it did it again, I deleted the page and recreated it, it still does it! this is the only script I'm running on this page while I get it to work. the code I'm using is below, can you see anything? it worked fine the first time except for the not loading thing <html> <head> <title>SEARCH RECORDS</title> </head> <body> <?php if( isset($_POST['Search']) ) { echo '////////////////// DEBUGGING CODE ////////////////////////////////////////////'; echo '<pre>'; print_r($_POST); echo '</pre>'; echo '/////////////////////// END DEBUGGING /////////////////////////////////////////'; } ?> <FORM NAME ="Search" METHOD ="POST" ACTION = "test2.php"> <INPUT TYPE = "TEXT" NAME = "surname"> <INPUT TYPE = "Submit" Name = "Search" VALUE = "Search"> </FORM> <?phpif( isset($_POST['Search']) ) { $user_name = "*****"; $password = "*****"; $database = "*****"; $server = "localhost"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "SELECT * FROM ***** WHERE surname LIKE '" . mysql_real_escape_string($_POST['surname']) . "%'"; $result = mysql_query($SQL) or die ('<br>Query string: ' . $SQL . '<br>Produced error: ' . mysql_error() . '<br>'); echo $SQL; // debugging output while ($db_field = mysql_fetch_assoc($result)) { print $db_field['Grave'] . "<BR>"; print $db_field['Surname'] . "<BR>"; print $db_field['Forenames'] . "<BR>"; print $db_field['Death_Date'] . "<BR>"; print $db_field['Birth_Year'] . "<BR>"; } mysql_close($db_handle); } else { print "Database NOT Found "; mysql_close($db_handle); } } else { echo '<br>(Form not yet submitted.)<br>'; // debugging output } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120364 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 Right after the </form> tag, the if() got pushed up a line <?phpif(, and it's cratering the syntax. Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120367 Share on other sites More sharing options...
diamondlifestyle Posted October 8, 2010 Author Share Posted October 8, 2010 fixed, thanks. I'm getting nothing but the same screen back when I submit a query. I think it must be an issue with a connection to the db, but am I right in thinking if it was something silly like putting in the wrong password it'd just kick me out and say 'db not available' as per the ELSE query? so I am connected, but the form and the db aren't interacting properly??? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120381 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 What is happening when you enter a name you know is in the database and submit the form? Are you getting the output from the debugging code at the top of the script? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120383 Share on other sites More sharing options...
diamondlifestyle Posted October 8, 2010 Author Share Posted October 8, 2010 no I'm just getting what looks like the refreshed page again. no debugging script and still says 'form not submitted' underneath Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120387 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 Take the first block out of the conditional if() and let it stand on its own. Let's see what the gives us. Something isn't making sense here. <?php echo '////////////////// DEBUGGING CODE ////////////////////////////////////////////'; echo '<pre>'; print_r($_POST); echo '</pre>'; echo '/////////////////////// END DEBUGGING /////////////////////////////////////////'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120393 Share on other sites More sharing options...
diamondlifestyle Posted October 8, 2010 Author Share Posted October 8, 2010 I've removed the if() clause completely, this now shows ////////////////// DEBUGGING CODE //////////////////////////////////////////// Array ( ) /////////////////////// END DEBUGGING ///////////////////////////////////////// when I entered a query (surname 'smith' as there are 2 of them in the db) it changed to: ////////////////// DEBUGGING CODE //////////////////////////////////////////// Array ( [surname] => smith ) /////////////////////// END DEBUGGING ///////////////////////////////////////// Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120400 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 OK. That confirms what I was thinking. The submit button's value isn't being set. That's a known issue in some browsers when the form is submitted with the enter key, rather than clicking on the button. Let's do this: Add a hidden field to the form. <INPUT TYPE = "Submit" Name = "Search" VALUE = "Search"><input type="hidden" name="submitted" value="yes"></FORM> Then . . . // change thisif( isset($_POST['Search']) ) { // to thisif($_POST['submitted'] == 'yes') { That should cure this whole thing. It's how I always check my own forms for submission. Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120404 Share on other sites More sharing options...
diamondlifestyle Posted October 9, 2010 Author Share Posted October 9, 2010 thank you so much for all your help, that looks like it's going to work (although I have a slight sql error I need to sort out first) but it's now 1.30am where I am so I'm giving up for tonight!! thanks again Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120437 Share on other sites More sharing options...
diamondlifestyle Posted October 9, 2010 Author Share Posted October 9, 2010 Hi again, I've been back on this again all morning and something's still going wrong; the page is loading up correctly, displaying the form with the text underneath 'enter surname to continue', however when a query is submitted in the form the page reloads, the text 'enter surname...' vanishes and the page expands down like there's something being displayed underneath the table, but there's nothing there. I'm assuming it's an error with the PRINT function; I've tried a couple of dozen different alterations (which usually just resulted in error messages) and I've also tried changing the WHILE query to an IF query (thinking that as my test query 'smith' isn't the first record it may be stopping the WHILE query before reaching the search results) but that just brings up an ELSE error which I can't seem to fix. I also wondered if the problem was the phrase '$db_field' in the below line if ($db_field = mysql_fetch_assoc($result)) { as it occurred to me that I haven't sepcified what $db_field is anywhere in the script; I've tried to add this on in a couple of different ways: adding $db_field = 'surname' at the top with the other login details amending the above script to if ($db_field ['Surname'] = mysql_fetch_assoc($result)) { but neither of them have worked. I'm really sorry to keep asking for help with this but nothing I'm trying is working. The full script I've got at the minute is: <FORM NAME ="Search" METHOD ="POST" ACTION = "test2.php"> <INPUT TYPE = "TEXT" NAME = "name"> <INPUT TYPE = "Submit" Name = "Search" VALUE = "Search"> <input type="hidden" name="submitted" value="yes"> </FORM> <?php if($_POST['submitted'] == 'yes') { $user_name = "*****"; $password = "*****"; $database = "*****"; $server = "localhost"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "SELECT * FROM ***** WHERE surname LIKE \'" . mysql_real_escape_string($_POST['name']) . "%'\"; $result = mysql_query($SQL) or die ('<br>Query string: ' . $SQL . '<br>Produced error: ' . mysql_error() . '<br>'); while ($db_field = mysql_fetch_assoc($result)) { print $db_field['ID'] . "<BR>"; print $db_field['Grave'] . "<BR>"; print $db_field['Surname'] . "<BR>"; print $db_field['Forenames'] . "<BR>"; print $db_field['Death_Date'] . "<BR>"; print $db_field['Birth_Year'] . "<BR>"; print $db_field['Inscription'] . "<BR>"; } mysql_close($db_handle); } else { print "Database Error "; mysql_close($db_handle); } } else { echo '<br>(Enter a surname to continue)<br>'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120535 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 You've apparently changed things that were already working. Right now, this line is causing parse errors: $SQL = "SELECT * FROM ***** WHERE surname LIKE \'" . mysql_real_escape_string($_POST['name']) . "%'\"; should be $SQL = "SELECT * FROM ***** WHERE surname LIKE '" . mysql_real_escape_string($_POST['name']) . "%'"; Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120538 Share on other sites More sharing options...
diamondlifestyle Posted October 9, 2010 Author Share Posted October 9, 2010 changing that just produces the same error, everything looks ok except no results are being displayed. Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120540 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 That script won't even run with the first query string; it causes parse errors. I just set up a test database table locally, and the script runs fine and produces the expected results. Is this script being include()d as part of another script? Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120546 Share on other sites More sharing options...
diamondlifestyle Posted October 9, 2010 Author Share Posted October 9, 2010 right, I set up a new page (test5) and a new db, and I've moved the script over to the new page and the new db, which only contains 4 entries (2 smiths and 2 jonses) while I test this out. When I search for 'smith' it prints '1' underneath the form (where 'enter surname...' is printed when the screen first loads) and when I search for 'jones' it prints '3'. When I enter a variable I know isn't in the db (e.g. Adams) it doesn't show anything. Quote Link to comment https://forums.phpfreaks.com/topic/215373-trying-to-search-a-mysql-db-using-php-code-searching-html-form/page/2/#findComment-1120567 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.