PDO准备的声明正确吗? [英] PDO prepared Statements correct?

查看:77
本文介绍了PDO准备的声明正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图第一次使用一些PDO准备好的语句来防止SQL注入.我对SQL还是很陌生,所以准备好的语句对我来说非常令人困惑.您认为我的带有准备好的语句的SQL代码是正确的吗?

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

    }  
    }   
    }   
    }        
    ?> 

这是我的旧" SQL代码(没有准备好的语句):

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

    }  
    }   
    }   
    }        
    ?> 

谢谢!

推荐答案

不应引用您的占位符.这使它成为文字字符串,而不是占位符.我也不知道getRows()getValue是什么.试试这个..

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

?是占位符,将在其中插入用户值. rowCount是PDO函数,用于查看查询返回了多少行. fetch是另一个拉取行的PDO函数.在PHP网站上有这些功能的参考和描述.

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.

我也不确定您是否应该在用户输入上运行htmlspecialchars.插入数据库后,数据库中的数据是否以这种方式转换?

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?

参考文献:

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

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

这篇关于PDO准备的声明正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆