简体   繁体   English

如何正确清理存储电子邮件的mssql查询

[英]How to correctly sanitize mssql query that stores emails

I have a query that goes to a mailbox and tries to save all emails in a table. 我有一个查询转到邮箱并尝试将所有电子邮件保存在表中。 It works in most cases but it fails when the email content value has double or single quote marks. 它适用于大多数情况,但在电子邮件内容值具有双引号或单引号时失败。 How would I modify my code to correctly insert all 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 INTO [tbl_test] (content) 
                    VALUES ('".$body."')";
            $result3 = mssql_query($sql3, $dbh1);
        }
    }

Afterwords I get these errors: 后来我得到这些错误:

Warning: mssql_query(): message: Unclosed quotation mark after the character string 'Please investigate why the below s.... 警告:mssql_query():message:字符串后面的未闭合引号'请调查以下为什么......

Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 15) in /var/www/testing.php on line 38 警告:mssql_query():常规SQL Server错误:在第38行的/var/www/testing.php中检查来自SQL Server的消息(严重级15)

You want to be using parameters: 您想要使用参数:

$query = "INSERT INTO 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 来源: sqlsrv_query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM