简体   繁体   English

如何为表中的所有查询创建动态php页面

[英]How make dynamic php page for all queries in table

I'm trying to retrieve dynamic query PHP pages. 我正在尝试检索动态查询PHP页面。 First page passing values of city or id next page by post method. 首页通过post方法传递城市或id的值的下一页。

I know here sql injection can done. 我知道这里可以进行sql注入。 but im using in it on private network and every user session is tracked. 但是我在专用网络上使用了它,并且跟踪了每个用户会话。 and table1 is protected. 并且table1受保护。

Table looks like this: 表看起来像这样:

TABLE1 表格1

-----------------------------------------------------
1     select * from tablename1 where ID = $id
2     select * from tablename1 where city = $city
2     select * from tablename1 where ID = $id  and city = $city
-----------------------------------------------------

Now I run page contain code: 现在我运行页面包含代码:

    $id = $_POST["id"];
    $city= $_POST["city"];
    $tabledata = mysql_query("SELECT * FROM table1 ");
    $a=mysql_fetch_row($tabledata );

    $query_table=$a['1']; // retrieve sql query from table1 which stored in 2nd column
    echo " $query_table"; // display query
    $result = mysql_query($query_table);

    while ($field = mysql_fetch_row( $result) )
    {
        echo "<td> $field[1] </td>";
    }

Now, the problem is that I can't get results. 现在,问题是我无法获得结果。 $result is empty? $result是否为空?

How pass $city or $id value to query? 如何传递$city$id值进行查询?

But if I remove WHERE condition from TABKE1 then it works well. 但是,如果我从TABKE1中删除WHERE条件,则它会很好地工作。 So, how can I make for WHERE condition to pass parameters? 那么,如何使WHERE条件传递参数?

how can i do this . 我怎样才能做到这一点 。 any other option here to call stored query on 1 table and process it on other page and oher table 此处的任何其他选项可以在1个表上调用存储的查询,并在其他页面和其他表上处理它

If you want the result with ID or City 如果您想要ID或City的结果

mysql_query("SELECT * FROM table1 WHERE ID = '$id' OR city = '$city'");

and If you want with both mandatory 并且如果您想同时具有强制性

mysql_query("SELECT * FROM table1 WHERE ID = '$id' AND city = '$city'");

The query in table1 should be like this table1中的查询应如下所示

-----------------------------------------------------
1     select * from tablename1 where ID = '".$id."'
2     select * from tablename1 where city = '".$city."'
3     select * from tablename1 where ID =  '".$id."'  and city =  '".$city."'
-----------------------------------------------------

and proceed further with your coding and it will show you the result. 并进一步进行编码,它将显示结果。

OPTION 2: 选项2:

Table 1: 表格1:

-----------------------------------------------------
1     select * from tablename1 where ID =  
2     select * from tablename1 where city =  
-----------------------------------------------------

and change in php code like this 并像这样更改php代码

$query_table=$a['1']." '".$id."'" ;

or 要么

$query_table=$a['2']." '".$city."'" ;

OPTION 3: 选项3:

Table 1: 表格1:

-----------------------------------------------------
1     select * from tablename1  

-----------------------------------------------------

and change in php code like this 并像这样更改php代码

$query_table=$a['1']." where ID = '".$id."'" ;

or 要么

$query_table=$a['1']." where ID = '".$id."' AND city = '".$city."' " ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM