I am new to DBI and MySQL in general. I am trying to take this hash and stores the keys in a MySQL database using DBI.
$VAR14 = { 'image_medium' => 'http://ecx.images-amazon.com/images/I/51 +p2cN2yIML._SL160_.jpg', 'authors' => 'Tony Stubblebine', 'publication_date' => '2007-07-18', 'title' => 'Regular Expression Pocket Reference: Regular Ex +pressions for Perl, Ruby, PHP, Python, C, Java and .NET (Pocket Refer +ence (O\'Reilly))', 'isbn' => '0596514271', 'publisher' => 'O\'Reilly Media, Inc.', 'image_small' => 'http://ecx.images-amazon.com/images/I/51p +2cN2yIML._SL75_.jpg', 'url_amazon' => 'http://www.amazon.com/Regular-Expression-P +ocket-Reference-Expressions/dp/0596514271%3FSubscriptionId%3D1VD4TGW1 +VSXT1RE5Y2G2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D1 +65953%26creativeASIN%3D0596514271', 'pages' => '126' };
When I try to add strings with quotes, I receive SQL syntax errors (i.e. the "Publisher" key and keys with "O'Reilly"). What is the best way to handle quotes in this scenario? This is how I am doing the insert
$insert = "INSERT into books ( id, title, authors, isbn, pages, publisher, publication_date, image_small, image_medium, url_amazon, createdAt) VALUES ( NULL, '" . $hash{$key}{'title'} . "', '" . $hash{$key}{'authors'} . "', '" . $hash{$key}{'isbn'} . "', '" . $hash{$key}{'pages'} . "', '" . $hash{$key}{'publisher'} . "', '" . $hash{$key}{'publication_date'} . "', + '" . $hash{$key}{'image_small'} . "', '" . $hash{$key}{'image_medium'} . "', '" . $hash{$key}{'url_amazon'} . "', NULL)"; $sth = $dbh->prepare($insert);
Thanks!

In reply to Best way to deal with quotes in string with DBI by ewhitt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.