Display MYSQL Data From a Menu

2019-03-04 17:43发布

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;
 }

 ?>

2条回答
Juvenile、少年°
2楼-- · 2019-03-04 17:50

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.

查看更多
你好瞎i
3楼-- · 2019-03-04 18:08

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;
 }

 ?>
查看更多
登录 后发表回答