2012-04-24 22 views
0

無論何時使用PHP導出CSV文件,我都會遇到問題。我有一個像使用CSV文件進行字符轉換

SELECT `interest_id`,`interest_name`,`interested_user_id` FROM `interest_list` WHERE `interest_name` LIKE '%fashion%' 

但每當一個MySQL查詢我試圖通過利用上述MySQL查詢導出一個PHP文件,然後我查詢轉向

SELECT `interest_id`,`interest_name`,`interested_user_id` FROM `interest_list` WHERE `interest_name` LIKE 'úshion%' 

,使其返回沒有記錄。 請告訴我如何解決這個問題。 在出口文件,我這樣寫代碼:

header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=data.csv'); 
$output = fopen('php://output', 'w'); 

//executed code 

fputcsv($output, $blankArray); 

現在,請幫助我。

其實,我使查詢在一個文件中,如:

$search_sql = "SELECT `interest_id`,`interest_name`,`interested_user_id` FROM `interest_list`"; 

    if(isset($_REQUEST['search_user_btn'])){ 

     if($_REQUEST['search_intr'] != '') 
$search_sql .= " WHERE `interest_name` LIKE '%".$_REQUEST['search_intr']."%' "; 

    }                     

然後在同一個文件中我寫的一樣:

<a href="export.php?report=interest&qur=<?php echo $search_sql; ?>"><button>Export to CSV</button></a> 

終於在export.php

header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=data.csv'); 
$output = fopen('php://output', 'w'); 

我想獲得$ _REQUEST ['qur']的值。但無法獲得$ _REQUEST ['qur']的原始值。

我想現在它是有道理的。 請幫助....

+0

你如何確定查詢已經改變?請顯示您使用的確切代碼。 – deceze 2012-04-24 07:48:01

+0

將代碼顯示在生成SQL的位置並執行它會更有幫助。 – 2012-04-24 07:49:20

+0

請再次看到上面的問題,我修改了。 – John 2012-04-24 07:52:55

回答

1

您不能在URL中插入隨機字符串,因爲有些字符在URL中有特殊含義。在PHP中,您可以使用rawurlencode()對URL參數的值進行編碼。同樣,由於相同的原因,您不能在HTML字符串中插入隨機字符。

<?php 

$url = 'export.php?report=interest&qur=' = rawurlencode($search_sql); 

?> 
<a href="<?=htmlspecialchars($url)?>"><button>Export to CSV</button></a> 

無論如何,在URL中傳遞SQL都會導致黑客入侵。

+0

好的非常感謝。 – John 2012-04-24 08:05:12

相關問題