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
https://forums.phpfreaks.com/topic/147454-php-help/
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
https://forums.phpfreaks.com/topic/147454-php-help/#findComment-774186
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
https://forums.phpfreaks.com/topic/147454-php-help/#findComment-774280
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.