Display MYSQL Data From a Menu

孤街醉人 提交于 2020-01-11 14:23:41

问题


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.



来源:https://stackoverflow.com/questions/10326077/display-mysql-data-from-a-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!