简体   繁体   English

mysql_query结果在第二篇文章中可用?

[英]mysql_query result available on second post?

I am using a dynamically generated query string to display results from a search form for some reports - there are 5 search fields and $query2 could contain nothing or 5 additional search values. 我使用动态生成的查询字符串来显示某些报告的搜索表单结果 - 有5个搜索字段,$ query2可能不包含任何内容或5个额外的搜索值。

$query="SELECT * from employee_work where company_id='$company_id' $query2";

When I POST the form I get the data displayed on screen which is great. 当我发布表单时,我会在屏幕上显示数据,这很棒。 I also then am using TCPDF to offer a PDF download of the data. 我也正在使用TCPDF提供数据的PDF下载。 I currently also the class for TCPDF via POST: 我目前也是通过POST的TCPDF类:

if($_POST['PDF']) {
    do the TCPDF stuff......
}

<div id="export-buttons">
<form name="export" method="post" action="">
<input type="submit" name="PDF" value="PDF" class="button pdf">
</div>

The problem is that when a user clicks on the PDF button the POST array now only contains the value for the submit button and NOT the $query2 data from earlier so when I use TCPDF it outputs all the data and ignores the $query2 part of the search string. 问题是当用户点击PDF按钮时,POST数组现在只包含提交按钮的值而不包含之前的$ query2数据,因此当我使用TCPDF时,它输出所有数据并忽略$ query2部分的搜索字符串。

How can I address this so either the original $query2 data stays available OR have anther way of checking if the form button has been clicked without overwriting the contents of POST? 我如何解决这个问题,以便原始的$ query2数据保持可用,或者有另一种方法来检查表单按钮是否被单击而不覆盖POST的内容? Can I use javascript to do this? 我可以用javascript来做这个吗?

Thanks Jason 谢谢杰森

why dont you add hidden inputs that have the values of the criteria from the search? 为什么不添加具有搜索条件值的隐藏输入? That way when the user posts the request for the PDF you get also those fiels and can use them (AFTER making sure the values are SAFE) in the query. 这样,当用户发布PDF请求时,您也会获得这些fiels并可以在查询中使用它们(确保值为SAFE之后)。

Other safer way is to store in the session an object or array with the parameters that made the list and then pass the identifier of that search as a hidden input to the PDF form, like: 其他更安全的方法是在会话中存储一个对象或数组,其中包含创建列表的参数,然后将该搜索的标识符作为隐藏的输入传递给PDF表单,如:

$_SESSION['sc0000001'] = array('field1'=>'value1', 'field2'=>'value2', 'field3'=123);
...
<form>
<input type="hidden" name="sc" value="0000001" />
...
</form>

when you post the form you get the identifier of the search and create the query with the criteria assign to session... 当您发布表单时,您将获得搜索的标识符并创建具有分配给会话的条件的查询...

EDITED: html before posting list criteria: 编辑:发布列表标准前的HTML:

<form>
<input type="text" name="filter1" value="" />
<input type="text" name="filter2" value="" />
<input type="text" name="filter3" value="" />
<input type="text" name="filter4" value="" />
<input type="text" name="filter5" value="" />
...
<input type="submit" value="go go go" />
</form>

PHP that gets the filters, builds query, gets results and stores in session. 获取过滤器,构建查询,获取结果和存储在会话中的PHP。

$sql = " select * from table_name where 1 ";

$arrFilters = array();
for($i=1;isset($_POST['filter'.$i]) && trim($_POST['filter'.$i])!="";$i++) { 
$arrFilters['filter'.$i] = mysql_real_escape_string($_POST['filter'.$i]);
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters supplied

// lets save this search, we are going to keep only the last search from this user in his session.
$_SESSION['listingFilters'] = $arrFilters;

HTML with search results and after the form to get pdf: 带搜索结果的HTML以及获取pdf的表单之后:

<form>

<input type="submit" value="get pdf" />
</form>

PHP: After the post to get the pdf we go check if there are filters PHP:在获得pdf的帖子后,我们去检查是否有过滤器

$sql = " select * from table_name where 1 "; // basic query
// get the filters array from session
$arrFilters = isset($_SESSION['listingFilters']) ? $_SESSION['listingFilters'] : array();
foreach($arrFilters as $filter => $value) { // for each filter add it to the query
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters in session
// and can do your pdf magic

Add pepper and salt to your pleasure (you need to revise the code to work for you and maybe also the query if you are using text has filters) 添加辣椒和盐(您需要修改代码以便为您工作,如果您使用的是文字,还可以查询过滤器)

Without seeing more code, this is probably because you have two "forms" on the HTML page, with one submit button in one form, and another in the other form. 没有看到更多代码,这可能是因为HTML页面上有两个“表单”,一个表单中有一个提交按钮,另一个表单中有另一个。

<form>
   <input name="1" />
   <input type="submit" name="go"/>
</form> 
<form>
   <input type="submit" name="createPDF" />
</form> 

Make sure you have all the fields/buttons inside the one form. 确保在一个表单中包含所有字段/按钮。

<form>
   <input name="1" />
   <input type="submit" name="go"/>
   <input type="submit" name="createPDF" />
</form> 

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

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