如何根据表单名称显示数据库结果

时间:2015-01-29 09:44:02

标签: php mysql

我正在构建一个属性网站,并开发了设置插入查询的页面来输入所需的数据。我可以根据URL显示基于id和其他信息的数据。在主页面上,我有一个表格,我需要显示结果。

数据库类型 - innodb

相关数据库结构

Table | Fields
property | id, name, description, developer_id,agent_id,area_id, price, bed, bath, type_id
agent |id,agent
developer |id, developer
area | id, city_id, area
city | id, country_id, city
country | id, country
type | id, type

数据库已保持连接状态,因此属性表字段与其对应关系按照field_id与table.id使用PHPMyAdmin关系管理器

我可以使用_isset_get来显示基于网址的结果,因为这是我展示另一个列出当前所选媒体资源的页面的方式,但对于此表单,将没有特别的url定义,因为它将显示在许多不同类型的页面上。

在旁注,我使用jqtransform,所以一些元素是伪css而不是实际的按钮,输入。

包含变量的表单示例

 <?php

require 'connect.php';
$title="Property";


$table="property";
$table2="developer";
$table3="agent";
$table4="area";
$table7="type";

$col1="name";
$col2="developer";
$col3="agent";
$col4="area";
$col5="size";
$col6="furnished";
$col7="type";
$col8="finished";
$col9="delivery";
$col10="price";
$col11="bed";
$col12="bath";
$col13="pool";
$col14="featured";
$col15="img";
$col16="imgname";



$title2="Developer";
$title3="Agent";
$title4="Area";
$title5="Size";
$title6="Furnished?";
$title7="Type of Property";
$title8="Finished Project?";
$title9="Delivery Date";
$title10="Price";
$title11="number of Bedrooms";
$title12="number of Bathrooms";
$title13="Pool available?";
$title14="Featured";
$title15="Upload Image (jpg,png,gif)";


$qry=mysql_query("SELECT * FROM $table", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>

<div class="grid_4">
        <div class="left-1">
            <h2 class="top-1 p3">Find a property</h2>
            <form id="form-1" class="form-1 bot-1" action="prop_result.php">
                <div class="select-1">
                    <label>Select Area</label>
                    <select name="field4" id="field4" >
    <?php



    $qry2=mysql_query("SELECT * FROM $table4", $con);
if(!$qry2)
{
die("Query Failed: ". mysql_error());
}



    while($row=mysql_fetch_array($qry2))
    {
echo "<option value='".$row[$col4]."'>".$row[$col4]."</option>";
    }
    ?>
                    </select>   
                </div>
                <div class="select-1">
                    <label>Property type</label>
                    <select name="field7" id="field7">
    <?php



    $qry2=mysql_query("SELECT * FROM $table7", $con);
if(!$qry2)
{
die("Query Failed: ". mysql_error());
}



    while($row=mysql_fetch_array($qry2))
    {
echo "<option value='".$row[$col7]."'>".$row[$col7]."</option>";
    }
    ?>
                    </select>   
                </div>
                          <div class="select-1">
                    <label>Price</label>
                    <input name="field10" type="text" id="field10" value="Type in Price" onBlur="if(this.value=='') this.value='Type in Price'" onFocus="if(this.value =='Address, City, Zip' ) this.value=''"  />
                </div>

                <div class="select-2">
                    <label>Beds</label>
                    <select name="field11" id="field11" >
                       <?php
 for ($i=1; $i<=10; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    }
    ?>
    </select>
                </div>
                <div class="select-2 last">
                    <label>Baths</label>
                    <select name="field12" id="field12">
                        <?php
 for ($i=1; $i<=10; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    }
    ?>
    </select>
                </div> 
                <a onClick="document.getElementById('form-1').submit()" class="button">Search</a>
                <div class="clear"></div>
            </form>

prop_result.php

<?php


//$field=$_POST['field'];
//$field2=$_POST['field2'];
//$field3=$_POST['field3'];
$field4=$_POST['field4'];
//$field5=$_POST['field5'];
//$field6=$_POST['field6'];
//$field7=$_POST['field7'];
$field8=$_POST['field8'];
//$field9=$_POST['date'];
$field10=$_POST['field10'];
$field11=$_POST['field11'];
$field12=$_POST['field12'];
//$field13=$_POST['field13'];
//$field14=$_POST['field14'];
//$field17=$_POST['field17'];


$qry_main=mysql_query("SELECT a.id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity,city_id, d.area as parea, size, furnished, h.type as ptype, finished, bed, bath, pool, featured from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id where a.area_id='$field4'",$con);
$qry_pic=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id'", $con);
$qry_pic1=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id' limit 1", $con);

if(!$qry_main)
{
die("Query Failed: ". mysql_error());
}





while($row=mysql_fetch_array($qry_main))
{
echo "<h2 class='top-1 p3'>Other ".$row['ptype']." properties in ".$row['pcity']."</h2>";

}

/*
isset() is used to check wheather arctile id is received through url from "index.php" file and if it is set corresponding arctile is displayted using SELECT statement.
*/
echo "<div class='facts_container'>";

$qry=mysql_query("SELECT b.city_id as city_id,type_id FROM property a left join area b on a.area_id=b.id WHERE a.id='$id'", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
                /* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry)){
$city=$row['city_id'];
$type=$row['type_id'];

$qry=mysql_query("SELECT a.id,city_id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity, d.area as parea, size,price, furnished, h.type as ptype, finished, bed, bath, pool, featured from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id WHERE city_id='$city' AND type_id='$type' AND a.id!='$id' order by a.id DESC", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}

while($row=mysql_fetch_array($qry))

{
echo "<a href='showprop.php?id=".$row['id']."'><div class='related_prop'>";
echo "<span class='medtext'>".$row['name']." <br/>".$row['ptype']."<br />";
echo $row['bed']." Bed | ".$row['bath']." Bath<br /><span class='medbold'>".$row['price']."</span></span><br/>";

$id=$row['id'];

$qry_pic1=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id' limit 1", $con);

if(!$qry_pic1)
{
die("Query Failed: ". mysql_error());
}
while($row=mysql_fetch_array($qry_pic1))
{
echo "<img src='uploads/".$row['aimage']."' /><br />";
}
echo "</div></a>";

}
}

?>

</div>

出现错误,说明字段未定义,尽管我已经在每种情况下都命名了

基本上我需要告诉prop_result.php已经提交了名为form-1的表单并开始工作,但是它甚至拒绝承认我定义的字段是存在的。我是否必须使用isset(_$post)验证所有字段,或者我可以设置一个声明,说明这个名为form-1的表单是否执行此操作。

ps:我知道代码需要大量清理,现在我已经注释掉了我现在不会使用的字段,并会根据需要添加,但是现在我只需要运行查询和根据提交的表格显示结果。

如果您需要任何其他信息以便在我的问题上帮助我,请告诉我。

--------------更新---------

阅读其中一个答案后的新代码。我做了一个隐藏的输入并将其用作isset_post。但现在它的空白没有错误

<?php
include 'connect.php';

if(isset($_post['form'])){

// $field4 = isset($_POST['field4']) ? $_POST['field4 '] : '';

//$field8 = isset($_POST['field8']) ? $_POST['field8'] : '';

//$field10 = isset($_POST['field10']) ? $_POST['field10'] : '';

//$field11 = isset($_POST['field11']) ? $_POST['field11'] : '';

//$field12 = isset($_POST['field12']) ? $_POST['field12'] : '';

//$field=$_POST['field'];
//$field2=$_POST['field2'];
//$field3=$_POST['field3'];
$field4=$_POST['field4'];
//$field5=$_POST['field5'];
//$field6=$_POST['field6'];
//$field7=$_POST['field7'];
//$field8=$_POST['field8'];
//$field9=$_POST['date'];
$field10=$_POST['field10'];
$field11=$_POST['field11'];
$field12=$_POST['field12'];
//$field13=$_POST['field13'];
//$field14=$_POST['field14'];
//$field17=$_POST['field17'];


$qry_main=mysql_query("SELECT a.id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity,city_id, d.area as parea, size, furnished, h.type as ptype, finished, bed, bath, pool, featured from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id where a.area_id='$field4'",$con);
$qry_pic=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id'", $con);
$qry_pic1=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id' limit 1", $con);

if(!$qry_main){
die("Query Failed: ". mysql_error());
}

while($row=mysql_fetch_array($qry_main)){
echo "<h2 class='top-1 p3'>Other ".$row['ptype']." properties in ".$row['pcity']."</h2>";


/*
isset() is used to check wheather arctile id is received through url from "index.php" file and if it is set corresponding arctile is displayted using SELECT statement.
*/
echo "<div class='facts_container'>";

$qry=mysql_query("SELECT b.city_id as city_id,type_id FROM property a left join area b on a.area_id=b.id WHERE a.id='$id'", $con);
if(!$qry){
die("Query Failed: ". mysql_error());
}
                /* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry)){
$city=$row['city_id'];
$type=$row['type_id'];

$qry=mysql_query("SELECT a.id,city_id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity, d.area as parea, size,price, furnished, h.type as ptype, finished, bed, bath, pool, featured from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id WHERE city_id='$city' AND type_id='$type' AND a.id!='$id' order by a.id DESC", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}

while($row=mysql_fetch_array($qry))
{
echo "<a href='showprop.php?id=".$row['id']."'><div class='related_prop'>";
echo "<span class='medtext'>".$row['name']." <br/>".$row['ptype']."<br />";
echo $row['bed']." Bed | ".$row['bath']." Bath<br /><span class='medbold'>".$row['price']."</span></span><br/>";

$id=$row['id'];

$qry_pic1=mysql_query("SELECT a.image as aimage,a.property_id,b.id from images a right join property b on b.id=a.property_id where b.id='$id' limit 1", $con);

if(!$qry_pic1){
die("Query Failed: ". mysql_error());
}
while($row=mysql_fetch_array($qry_pic1)){
echo "<img src='uploads/".$row['aimage']."' /><br />";
}
echo "</div></a>";
}   
}
}
}

?>

</div>

1 个答案:

答案 0 :(得分:2)

如果您的PHP上有多个表单,则必须包含一个元素或其他内容(例如隐藏的div或输入元素),其中包含有关提交的表单的信息。在提交页面之前将其设置为1或表单名称。

您还可以检查表单ID上的isset-POST,以便在表单提交时将其设置为true。如果表单已提交,请检查其他字段。

编辑:我注意到在您更新的代码中,对表单元素的引用不包括POST方法。事实上,你的html中没有地方提到POST。您必须提及表单是应该作为GET还是POST提交。

更改:

<form id="form-1" class="form-1 bot-1" action="prop_result.php">

致:

<form id="form-1" class="form-1 bot-1" method = "POST" action="prop_result.php">