2016-08-01 59 views
-1

計劃是創建一個面向公衆的互聯網服務,可以從數據庫中自動填充單詞。我使用普通的mysql查詢成功地工作,但我擔心,因爲我將把這個連接包含到一個wordpress插件中,這會使我的服務器容易受到攻擊。SQL注入證明SELECT和LIKE以及通配符到關聯數組中

我在MYSQL中創建了一個新用戶,只有這張表的選擇權限,所以我不擔心在腳本中公開密碼,因爲我相信這將有助於調試。有各種回聲等我放在那裏用於調試目的。最終這個輸出將進入Json文件並通過Jquery UI自動完成進行檢索。

本質上,我遇到的問題是我無法使查詢工作。我嘗試逐行添加代碼,但連接不起作用。隨意在mysql工作臺上測試憑據,IP地址是46.101.8.220。更難調試的是,代碼中的某個人阻止從Web瀏覽器查看時在頁面輸出中出現的任何內容。任何幫助將不勝感激,因爲我花了很多時間嘗試Mysqli,PDO,面向對象和程序試圖讓這個工作。

$term = isset($_GET['term']) ? $_GET['term'] . '%': 'test%'; 
echo $term; 

$conn = new mysqli('localhost', 'search-words','','admin_soup'); 
print ($conn ? 'connected' : 'cant con'); 
$query = 'SELECT id as value, word as label FROM word where word LIKE ?;'; 
    $stmt = $conn->prepare($query); 
    $stmt->bind_param("s", $term) 
    if ($stmt->execute()){ 
     $result = $stmt->get_result(); 
     while($row = $result->fetch_array(MYSQLI_ASSOC)){ 
      $row_set[] = $row; 
     } 
    } 
echo json_encode($row_set);//format the array into json data 
$stmt->close(); 
$conn->close(); 
print_r($row_set); 

在第一個建議代碼改變並工作之後。謝謝!

$term = isset($_GET['term']) ? $_GET['term'] . '%': 'test%'; 
echo $term; 
$conn = new mysqli('localhost', 'search-words','','admin_soup'); 
print ($conn ? 'connected' : 'cant con'); 
$query = 'SELECT id as value, word as label FROM word where word LIKE ?;'; 
    $stmt = $conn->prepare($query); 
    $stmt->bind_param("s", $term); 
    if ($stmt->execute()){ 
     $stmt->bind_result($value,$label); 
     while($row = $stmt->fetch()){ 
      $row_set[] = array('value'=>$value, 'label'=>$label); 
     } 
    } 

echo json_encode($row_set);//format the array into json data 
$stmt->close(); 
$conn->close(); 

該代碼仍然不會返回任何內容。仔細檢查用戶信息。當我測試sql語句時,唯一需要更改的是我需要圍繞'test%'添加quoations,但是我認爲bind_param不需要引號。如果我從$查詢downwars中刪除所有的代碼,它會在屏幕上顯示連接的消息,如果我再次添加下面。屏幕上什麼都沒顯示。

有沒有可能需要更新我的服務器或東西?

回答

0
$stmt->bind_param("s", $term) 

缺少「;」

$result = $stmt->get_result(); 

get_result()方法並不總是可用的。檢查php.net:http://php.net/manual/en/mysqli-stmt.get-result.php

只適用於mysqlnd。

這就是爲什麼PDO是這樣就方便多的原因之一......

但我猜你可以使用bind_result()然後fetch()數據如圖所示的例子http://php.net/manual/en/mysqli-stmt.fetch.php

if ($stmt->execute()){ 
    $stmt->bind_result($value, $label); 
    while($row = $stmt->fetch()){ 
     $row_set[] = array('value' => $value, 'label' => $label); 
    } 
} 

通過使用PDO代碼可能看起來像這樣:

$stmt = $dbh->prepare('SELECT id as value, word as label FROM word where word LIKE ?'); 
$stmt->execute(array($term)); 
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); 
+0

我試過mysqli解決方案,stilll沒什麼,我用一些新的想法更新了這個問題。我開始懷疑這是否與代碼無關。 –

+0

@StephenMclaughlin'$ row_set [] = array('value'=> $ value,'label'=> $ label)'Missing;再次。 – Philipp

+0

看不到樹林:'( –