Jump to content

PHP Help


BlackWidow

Recommended Posts

The script below keeps returning "Query was Empty".

 

Please can anyone advise why, I have been raking my brains for ages.  ???

 

<?php

include "connect.php";

?>

<h2>Search births database</h2>

 

<form action="search_back.php" method="post">

Search Surname: <input type="text" name="surname" /><br />

Search Forenames: <input type="text" name="forename" /><br />

<input type="hidden" name="searching" value="yes" />

<input type="submit" name="submit" value="Submit" />

</form>

 

<?php

if($searching == "yes")

{

  echo $_POST['surname'];

  echo $_POST['forename'];

  echo "<h2>Results</h2>";

 

if ($surname == "")

{

echo "<p>You forgot to enter a search criteria</p>";

exit;

}

 

// connect to Database

require_once ('connect.php'); // Connect to the database

 

      $surname = $_POST['surname']; 

      $forename = $_POST['forename'];

 

$sql=mysql_query("SELECT birth.year, birth.surname, birth.forename, birth.district,

                            birth.church, birth.abode, birth.dob,

                            birth.bapDate, birth.parent, birth.fatherOccup, birth.note,

    FROM birth

                            WHERE surname LIKE '%surname%'

            AND forename LIKE '%forename%'");

 

$result = mysql_query($query) or die(mysql_error());

 

while ($result=mysql_fetch_array($sql))

{

echo 'Year: '.$row['year'];

echo 'Surname; '.$row['surname'];

echo 'Forename: '.$row['forename'];

echo 'District: '.$row['district'];

echo 'Qtr: '.$row['qtr'];

echo 'Vol: '.$row['vol'];

echo 'Page: '.$row['page'];

echo 'Chruch: '.$row['church'];

echo 'Town: '.$row['town'];

echo 'Abode: '.$row['abode'];

echo 'DOB '.$row['dob'];

echo 'Bap Date: '.$row['bapDate'];

echo 'Parent:  '.$row['parent'];

echo 'Father Ocup: '.$row['fatherOccup'];

echo 'Notes: '.$row['notes'];

echo 'Reg At: '.$row['regAt'];

echo 'Sub Dist '.$row['subDist'];

echo 'Ref: '.$row['ref'];

}

$anymatches=mysql_num_rows($sql);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query<br><br>";

}

 

//And we remind them what they searched for

//echo "<b>Searched For:</b> " .$surname, $forename;

}

?>

Link to comment
Share on other sites

Sorry but this code is pretty well all over the place. $searching and $surname in this part....

 

if($searching == "yes")
{
   echo $_POST['surname'];
   echo $_POST['forename'];
   echo "<h2>Results</h2>";

if ($surname == "")
{
echo "<p>You forgot to enter a search criteria</p>";
exit;
}

 

You need to reference them from the $_POST array.

 

You also never actually use any user inputed data in your query.

 

$surname = mysql_real_escape_string($_POST['surname']);;  
$forename = mysql_real_escape_string($_POST['forename']);;
$sql=mysql_query("SELECT birth.year, birth.surname, birth.forename, birth.district, 
                            birth.church, birth.abode, birth.dob, 
                            birth.bapDate, birth.parent, birth.fatherOccup, birth.note, 
             FROM birth
                            WHERE surname LIKE '%$surname%'
                  AND forename LIKE '%$forename%'");

Link to comment
Share on other sites

Also you need to change

$result = mysql_query($query) or die(mysql_error());

while ($result=mysql_fetch_array($sql))
{

Into

$result = mysql_query($query) or die(mysql_error());

while ($row=mysql_fetch_array($result))
{

 

Because you use echo 'Year: '.$row['blablabla']; and not $result['blablabla'] Also notice that the aray fetched was changed from $sql to $result

 

 

 

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.