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


how to show database results based on name of form

我正在构建一个房地产网站,并开发了设置插入查询以输入所需数据的页面。我可以根据 url 根据 id 和其他信息显示数据。在主页上,我有一个需要显示结果的表单。

数据库类型 - innodb

相关数据库结构

表 |领域物业 |ID, 姓名, 描述, developer_id,agent_id,area_id, 价格, 床, 浴室, type_id代理 |ID,代理开发者 |id, 开发者面积 |ID, city_id, 区域城市 |ID, country_id, 城市国家 |ID, 国家类型 |id, 类型

数据库已保持连接状态,因此属性表字段与它们的对应项有关系,根据使用 PHPMyAdmin 关系管理器 table.id field_id

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

附带说明一下,IM 使用 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>

如果你的 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">