SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Confused by PDO bindValue

  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,122
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Confused by PDO bindValue

    I used to write the code like this:

    Code:
    if (isset($_POST['ad']))
    	$ad = $_POST['ad'];
    	$ad = htmlspecialchars($ad, ENT_QUOTES, 'UTF-8');
    But if I do this:

    Code:
    try
    {
    $sql = "INSERT INTO store" SET
    rob = :rob";
    
     $s = $pdo->prepare($sql);
     $s->bindValue(':rob', $_POST['rob']);
     $s->execute();
    }
    catch (PDOException $e)
    {
    $output = 'Error performing update: ' . $e->getMessage();
    include 'output.php';
    exit();
    ... then does that mean I re-write the top part as just:
    Code:
    if (isset($_POST['ad']))
    ... dropping the htmlspecialchars() line?

    Does bindValue mean we don't need to use htmlspecialchars() any more? I'm redoing my code with PDO and need clarification on this point. Is htmlspecialchars() just used for echoing data?

    Thanks!
    ----------------------------
    Steve Husting
    http://iphonedevlog.wordpress.com/

  2. #2
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,769
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    You really never need to use htmlspecialchars when inserting into a database, you may need to use it when outputting content stored in the database. htmlspecialchars will prevent XSS (Cross Site Scripting) attacks, someone trying to inject malicious JavaScript or markup into your site.

    So yes, you don't need to call htmlspecialchars when inserting into your database, however, you may want to use it when outputting anything from your database. Therefore, if you want to limit your code changes, and since you have been running it through htmlspecialchars up to this point, you may as well keep that line before inserting your record (it really won't harm anything).

  3. #3
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    htmlspecialchars is actually completely unrelated to anything SQL.

    The gist is:
    - When using user provided content in SQL, then escape characters that are special to SQL (real_escape_string, prepare/bind).
    - When using user provided content in HTML, then escape characters that are special to HTML (htmlspecialchars).

    So regardless if you escape for SQL with real_escape_string or prepare/bind, you don't need htmlspecialchars.

    EDIT: I'm too slow.
    "Folks who know what they're doing make complexity seem simple."

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,122
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ooops. "ad" should be "rob"
    ----------------------------
    Steve Husting
    http://iphonedevlog.wordpress.com/

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,122
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you all!
    ----------------------------
    Steve Husting
    http://iphonedevlog.wordpress.com/

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,122
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, this PHP/MySQL book is telling me to use htmlspecialchars when echoing from the table:

    <p>
    <?php
    echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');
    ?>
    </p>
    ----------------------------
    Steve Husting
    http://iphonedevlog.wordpress.com/

  7. #7
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,769
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is technically the only time you need to use htmlspecialchars.

    However, in my prior response, I simply meant to say, since you were using it before inserting your data, you can continue to do that to keep your data in your database consistent and you will remain protected from XSS.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,122
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks again!
    ----------------------------
    Steve Husting
    http://iphonedevlog.wordpress.com/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •