PDO prepared Statements correct?

霸气de小男生 提交于 2020-01-07 08:24:06

问题


I am trying to use some PDO prepared statements for the first time to prevent a SQL Injection. I am quite new to SQL so the prepared statements are very bewildering to me. Do you think my SQL code with prepared statements is correct?

<?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>';   

    }  
    }   
    }   
    }        
    ?> 

This is my "old" SQL Code (without prepared statements):

<?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>';   

    }  
    }   
    }   
    }        
    ?> 

Thank you!


回答1:


Your placeholders shouldn't be quoted. That makes it a literal string, not a placeholder. I also don't know what getRows() and getValue are. Try this out..

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';
    }
}

The ?s are placeholders where the user values will be inserted. The rowCount is a PDO function to see how many rows the query returned. The fetch is another PDO function to pull the rows. There are references and write ups of these functions on the PHP site.

I'm also not sure you should be running htmlspecialchars on the user input. Was the data in the DB converted that way when inserted?

References:

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



来源:https://stackoverflow.com/questions/33214336/pdo-prepared-statements-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!