BlackWidow Posted March 1, 2009 Share Posted March 1, 2009 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; } ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted March 1, 2009 Share Posted March 1, 2009 $result = mysql_query($query) or die(mysql_error()); $query is never defined anywhere x.x. Quote Link to comment Share on other sites More sharing options...
BlackWidow Posted March 1, 2009 Author Share Posted March 1, 2009 Where do I define the query? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted March 1, 2009 Share Posted March 1, 2009 Where do I define the query?I'd guess that you're calling it $sql Quote Link to comment Share on other sites More sharing options...
BlackWidow Posted March 1, 2009 Author Share Posted March 1, 2009 I am calling it sql, I have tried changing it to $query and it still doesnt work, I must be misisng something. Quote Link to comment Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Share Posted March 1, 2009 First off put code in [ code ] tags please change this $result = mysql_query($query) or die(mysql_error()); to this $result = mysql_query($sql) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
BlackWidow Posted March 1, 2009 Author Share Posted March 1, 2009 Sorry, ok will do, Im new to this. Have changed code, still doesnt work. Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 Have changed code, still doesnt work. Your new error? And current code? Quote Link to comment Share on other sites More sharing options...
BlackWidow Posted March 1, 2009 Author Share Posted March 1, 2009 New code. I have changed the word "query" to "sql" as above, Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted March 1, 2009 Share Posted March 1, 2009 while ($row=mysql_fetch_array($result)) { Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 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%'"); Quote Link to comment Share on other sites More sharing options...
BlackWidow Posted March 1, 2009 Author Share Posted March 1, 2009 Thank you I will try that Quote Link to comment Share on other sites More sharing options...
Rodis Posted March 2, 2009 Share Posted March 2, 2009 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 Quote Link to comment 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.