Display MYSQL Data From a Menu

2019-03-04 18:00发布

问题:

I have a MYSQL database with a number of fields such as property, bedrooms, size etc

I have two dropdown list with data that is contained within the database

When submitting the options I want a new page to open displaying the results. I am getting the error message mysql_fetch_assoc(): supplied argument is not a valid MySQL and have no idea how to fix this! help much appreciated...I know about SQL injections and looking to rectify this after I get this section working first

HTML

     <form method="get" action="submit.php">

     Number:  <select name="property">
     <option value="Aviemore House">Aviemore House</option>
     <option value="Dalfaber House">Dalfaber House</option>
     </select>
     <br>

     Name: <select name="bedrooms">
     <option value="2">2</option>
     <option value="3">3</option></select>
     <br>

    <input type="submit" value="submit" />
    </form>

PHP

 <?php

 require 'defaults.php';
 require 'database.php';


 $property = $_GET['property'] ;
 $bedrooms = $_GET['bedrooms'] ;

 $query = "select FROM properties where property = '$property' & bedrooms = '$bedrooms'";

 while ($row = mysql_fetch_assoc($result))
 {
$r[] = $row;
 }

 ?>

回答1:

You forgot to execute your query!

<?php

 require 'defaults.php';
 require 'database.php';


 $property = $_GET['property'] ;
 $bedrooms = $_GET['bedrooms'] ;

 $query = "select FROM properties where property = '$property' & bedrooms = '$bedrooms'";
 $result = mysql_query($query); // <-- You forgot this
 while ($row = mysql_fetch_assoc($result))
 {
    $r[] = $row;
 }

 ?>


回答2:

Try this instead:

$query = "SELECT * FROM `properties` WHERE property = '{$property}' AND bedrooms = '{$bedrooms}'";
$row=mysql_query($query);

Your sql is malformatted and need to execute the query.