Jump to content

trying to search a mysql db using php code searching html form


diamondlifestyle

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

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 /////////////////////////////////////////';

?>

Link to comment
Share on other sites

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 /////////////////////////////////////////

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

 

 

Link to comment
Share on other sites

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']) . "%'";

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

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.