【问题标题】:how to show database results based on name of form如何根据表单名称显示数据库结果
【发布时间】:2015-01-29 09:44:02
【问题描述】:

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

数据库类型 - innodb

相关数据库结构

表 |字段 房产 | id、名称、描述、developer_id、agent_id、area_id、价格、床、浴缸、type_id 代理 |id,代理 开发者 |id, 开发者 面积 | id, city_id, 区域 城市| id, country_id, 城市 国家 |身份证,国家 类型 |身份证,类型

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

我可以使用 _isset_get 来显示基于 url 的结果,因为这是我显示另一个页面的方式,其中列出了当前选定的属性,但是对于这种形式,不会有特定的 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>

【问题讨论】:

  • 哪些字段未定义?
  • 注意:未定义索引:第 9 行 D:\xampp\htdocs\Websites\mindia\prop_result.php 中的字段 4 注意:未定义索引:D:\xampp\htdocs\Websites\mindia\ 中的字段8第 13 行的 prop_result.php 注意:未定义的索引:第 15 行的 D:\xampp\htdocs\Websites\mindia\prop_result.php 中的 field10 注意:未定义的索引:D:\xampp\htdocs\Websites\mindia\prop_result 中的 field11。第 16 行的 php 注意:未定义的索引:第 17 行 D:\xampp\htdocs\Websites\mindia\prop_result.php 中的 field12
  • 你真的不应该像这样构建现代 PHP 应用程序。将所有数据库调用、PHP 和 HTML 混合在一起只是糟糕的设计。我会重新考虑你解决这个问题的整个方法,否则你将永远追逐你的尾巴,试图让这件事正常工作。
  • 感谢@gnarly。请发送正确方法的示例,以便我可以使用它。
  • 我真的无法提供示例,我会查看 Twig 模板或利用适当的框架,例如 Symfony。 Symfony 使用 Twig 作为模板引擎。使用 Symfony 有一点学习曲线,但最终,您将拥有一个非常好的应用程序,它可以清晰地分离您的业务逻辑、数据模型和表示。查看 Symfony 或任何其他框架。让生活变得如此,如此轻松。他们让你摆脱琐碎的事情,思考更高的层次。

标签: php mysql


【解决方案1】:

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

【讨论】:

  • 我喜欢这种方法,但由于某种原因,没有任何错误,我的页面是空白的。使用新代码更新问题
  • 谢谢旋风,我也想通了,并为此踢了自己......如果你没有提到它,我会自己回答这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多