2015-10-19 120 views
-1

我想第一次使用一些PDO準備語句來防止SQL注入。我對SQL很陌生,所以準備好的語句對我來說非常困惑。你認爲我準備好的SQL語句是正確的嗎?PDO準備的語句正確嗎?

<?php 
    if ($_SERVER["REQUEST_METHOD"] == 'POST') { 

    $suche = htmlspecialchars($_POST['suche']); 

    $stmt->bindParam(':suche', $suche); 

    if (!empty($suche)) {  

    $sql = new rex_sql; 

    $sql->debugsql = 0; 

    $stmt = $sql->prepare("SELECT * FROM rex_downloads WHERE dateiname LIKE '%:suche%' OR projektnummer LIKE '%:suche%' OR teilnehmer LIKE '%:suche%'"); 

    $stmt->execute(); 

    if ($sql->getRows() >= 1) { 
    for($i = 1; $i <= $sql->getRows(); $i++, $sql->next()) 
    { 
     $dateiname_suche = $sql->getValue("dateiname"); 
     $datei_projektnummer_suche = $sql->getValue("projektnummer"); 
     $teilnehmer_suche = $sql->getValue("teilnehmer"); 
     $dateidatum_suche = date("d.m.Y",strtotime($sql->getValue("dateidatum"))); 
     $dateizeit_suche = date("H.i",strtotime($sql->getValue("dateidatum"))); 
     $datei_projektseite_suche = $sql->getValue("projektseite"); 

     $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>'; 

    } 
    } 
    } 
    }   
    ?> 

這是我的 「老」 的SQL代碼(沒有準備好的發言):

<?php 
    if ($_SERVER["REQUEST_METHOD"] == 'POST') { 

    $suche = htmlspecialchars($_POST['suche']); 

    if (!empty($suche)) {  

    $sql = new rex_sql; 

    $sql->debugsql = 0; 

    $sql->setQuery("SELECT * FROM rex_downloads WHERE dateiname LIKE '%$suche%' OR projektnummer LIKE '%$suche%' OR teilnehmer LIKE '%$suche%'"); 

    if ($sql->getRows() >= 1) { 
    for($i = 1; $i <= $sql->getRows(); $i++, $sql->next()) 
    { 
     $dateiname_suche = $sql->getValue("dateiname"); 
     $datei_projektnummer_suche = $sql->getValue("projektnummer"); 
     $teilnehmer_suche = $sql->getValue("teilnehmer"); 
     $dateidatum_suche = date("d.m.Y",strtotime($sql->getValue("dateidatum"))); 
     $dateizeit_suche = date("H.i",strtotime($sql->getValue("dateidatum"))); 
     $datei_projektseite_suche = $sql->getValue("projektseite"); 

     $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>'; 

    } 
    } 
    } 
    }   
    ?> 

謝謝!

+1

我建議你去*代碼審查*本網站是關於在社區的問題提供解決方案的編程問題。 * CodeReview *建議改進您的代碼。 – Script47

回答

2

您的佔位符不應被引用。這使它成爲一個文字字符串,而不是佔位符。我也不知道getRows()getValue是什麼。嘗試了這一點..

if ($_SERVER["REQUEST_METHOD"] == 'POST') { 
    $suche = '%' . htmlspecialchars($_POST['suche']) . '%'; 
    $stmt = $sql->prepare("SELECT * FROM rex_downloads WHERE dateiname LIKE ? OR projektnummer LIKE ? OR teilnehmer LIKE ?"); 
    $stmt->execute(array($suche, $suche, $suche)); 
    if ($stmt->rowCount() > 0) { 
     $suche_download_ausgabe = ''; 
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
      $dateiname_suche = $row['dateiname']; 
      $datei_projektnummer_suche = $row['projektnummer']; 
      $teilnehmer_suche = $row['teilnehmer']; 
      $dateidatum_suche = date("d.m.Y",strtotime($row['dateidatum'])); 
      $dateizeit_suche = date("H.i",strtotime($row['dateidatum'])); 
      $datei_projektseite_suche = $row['projektseite']; 
      $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>'; 
     } 
    } else { 
     echo 'No results'; 
    } 
} 

? s爲佔位符的用戶值將被插入。 rowCount是一個PDO函數,用於查看查詢返回的行數。 fetch是另一個PDO函數來拉行。 PHP站點上有這些函數的引用和寫法。

我也不確定您應該在用戶輸入上運行htmlspecialchars。數據庫中的數據是否在插入時以這種方式轉換?

參考文獻:

http://php.net/manual/en/pdostatement.rowcount.php
http://php.net/manual/en/pdostatement.fetch.php
http://php.net/manual/en/pdo.prepared-statements.php

+0

我使用的是「htmlspecialchars」,否則用戶可以在文本輸入中輸入一些HTML並銷燬該網站:/ – susanloek

+1

您不是在網站上的任何地方輸出「$ suche」,但您只是使用它來查詢數據庫。在數據庫中的值是轉換爲實體或是他們的字符。同樣''htmlspecialchars'你也應該編碼單引號。 – chris85