49890

trying to search through sql data shown in php, not working

Question:

Hi I have a SQL database which contains information about properties. Above this I have a search button which searches the propertyLocation field to display relevant ones.

Example:

Property ID 1, 5 Bedroom House, London Property ID 2, 3 Bedroom House, Scotland Property ID 3, 2 Bedroom Flat, Cardiff.

At the top all i'm trying to do is put a search in which if the user types "london" only property ID 1 will be shown. I cannot seem to get it to work! My code is as follows:

Displaying the properties and search box:

<table id="propertyTable"> <form method="post" action="search.php"> <input type="text" name="search" /> <input type="submit" name="submit" value=" Search "> </form> <th>Property ID</th> <th>Property Name</th> <?php $result = mysql_query("SELECT * FROM Property"); while ($row = mysql_fetch_array($result)) { $pID = $row['pID']; $pLocation = $row['pLocation']; echo "<tr data-row='$pID'><td>$pID</td>"; echo "<td>".$pID."</td>"; echo "<td>".$pLocation."</td>"; } ?> </table>

Search.php code:

<?php $sql="SELECT * FROM Property WHERE pLocation like '$search%'"; $result=mysql_query($sql, $db) or die(mysql_error()); while ($row=mysql_fetch_array($result)) { $pID=$row["pID"]; $pLocation=$row["pLocation"]; } ?>

Please note I am connecting to my DB at the top. The first form does display the properties however, search just does not work.

Any help would be greatly appreciated!

<ul><li>

I keep getting errors, Use of undefined constant search and undefined variable DB...

</li> <li>

Errors fixed, it leads to search.php but nothing is displayed! Current search.php code is:

"; echo "".$pID.""; echo "".$pLocation.""; } ?></li> </ul>

Answer1:

You need to get your $search var out of the $_POST array:

$search = $_POST['search'];

edit:

Saw your second error about DB. You said: "Please note I am connecting to my DB at the top. " but if you are getting $db undefined, there is an issue with this. Maybe follow the syntax on your other page and use the assumed connection like:

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

On the first page there is output echoed. You need to mimic that on your search results:

$pID = $row['pID']; $pLocation = $row['pLocation']; echo "<tr data-row='$pID'>"; echo "<td>".$pID."</td>"; echo "<td>".$pLocation."</td></tr>";

I tried to fix some of the wonky table structure; the first page seemed to output one long row?

Answer2:

Try the following, i.e. initialize the $search variable with the POST value

<?php $search = trim($_POST['search']); $sql="SELECT * FROM Property WHERE pLocation like '$search%'"; $result=mysql_query($sql, $db) or die(mysql_error()); while ($row=mysql_fetch_array($result)) { $pID=$row["pID"]; $pLocation=$row["pLocation"]; } ?>

Recommend

  • Cannot run the macro
  • IE readonly textarea problem
  • How to create a data template dependent on an XML Attribute?
  • css calendar - td background diagonal split - two colors
  • Confirm box return value from alertify plugin and jquery
  • Multiple flexboxes with margin-right, except the last one in the row? Without JS?
  • How to get Fully qualified domain name in unix
  • passing parameter to DownloadStringCompletedEventHandler in C#
  • Flex items with same property values are rendering in different sizes
  • Django Haystack Rebuild Index
  • Approximate Order-Preserving Huffman Code
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Keep this build forever option - Jenkins
  • How to get a value (ex: baseURL) in every Karate feature?
  • How to show dropdown in excel using jrxml (jasper api)?
  • jQuery tmpl and DataLink beta
  • Delete MySQLi record without showing the id in the URL
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Unanticipated behavior
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • SQL merge duplicate rows and join values that are different
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • Trying to get generic when generic is not available
  • How to stop GridView from loading again when I press back button?
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?