你只需要使用get参数在你的链接,如果我正确理解你的问题。但是,您不希望传递整个查询或将该查询保存到会话中。相反,您只是想传递查询所需的值。为了保持查询本身保持在一个地方,我会创建一些模型类或一组可以包含在需要它们的文件中的函数。
<?php
// initial page
$field1 = 'abc';
$field2 = 'xyz';
$query = "SELECT * FROM table1 where field1 = ? and feild2=?";
$query1 = "SELECT * FROM table2 where field1= ? and feild2= ?";
$query2 = "SELECT * FROM table3 where field1= ? and feild2= ?";
?>
<a target="_blank" href="<?php printf('file1.php?field1=%s&field2=%s', $field1, $field2) ?>" >Firstquery</a>
<a target="_blank" href="<?php printf('file2.php?field1=%s&field2=%s', $field1, $field2) ?>">seconquery</a>
<a target="_blank" href="<?php printf('file3.php?field1=%s&field2=%s', $field1, $field2) ?>">thirdquery</a>
然后在文件* .PHP
<?php
// File1.php
$field1 = isset($_GET['field1']) ? $_GET['field1'] : null;
$field2 = isset($_GET['field2']) ? $_GET['field2'] : null;
$query = "SELECT * FROM table1 where field1 = ? and feild2=?";
$query1 = "SELECT * FROM table2 where field1= ? and feild2= ?";
$query2 = "SELECT * FROM table3 where field1= ? and feild2= ?";
$db = new PDO($dsn, $user, $pass);
if ($field1 && $field2) {
// do your querying
// if you are not using PDO or mysqli which supports prepared statements then you
// need to manually quote these variables since the come from the frontend
// you also probably want to sanitize of validate them in some way as well.
$stmt = $db->prepare($query);
$stmt->execute(array($field1, $field2);
$results1 = $pdo->fetchAll(PDO::FETCH_ASSOC);
// rinse and repeat for the other 2 queries
} ?>
可能重复http://stackoverflow.com/questions/871858/php-pass-variable-to-next-页面) – 2014-10-29 16:37:13
您可以通过隐藏的输入类型传递该信息。 – Wiraj 2014-10-29 16:38:00
@Wiraj通过表单字段传递sql是一个非常糟糕的主意,任何人都可以修改它。 – jeroen 2014-10-29 16:48:00