php - How to correctly sanitize mssql query that stores emails -
i have query goes mailbox , tries save emails in table. works in cases fails when email content value has double or single quote marks. how modify code correctly insert queries?
$num = imap_num_msg($imap); if($num > 0) { for($b = $num; $b > 0; $b--) { $body = $this->get_part($imap, $b, "text/html"); if($body == "") { $body = $this->get_part($imap, $b, "text/plain"); } $header = imap_headerinfo($imap, $b); $subject = $header->subject; $fromaddress = $header->fromaddress; $body = str_replace("'", "''", $body); //$body = str_replace("\"", "\"\"", $body); $sql3 = "insert [tbl_test] (content) values ('".$body."')"; $result3 = mssql_query($sql3, $dbh1); } }
afterwords these errors:
warning: mssql_query(): message: unclosed quotation mark after character string 'please investigate why below s....
warning: mssql_query(): general sql server error: check messages sql server (severity 15) in /var/www/testing.php on line 38
you want using parameters:
$query = "insert test (email, body) values (?,?);"; $arrparams[]="my@domain.com"; $arrparams[]="my email body has quotes\'s or double quotes \" in it."; $resource=sqlsrv_query($conn, $query, $arrparams);
source: sqlsrv_query
Comments
Post a Comment