我有一个查询进入邮箱并尝试将所有电子邮件保存在表格中。它适用于大多数情况,但在电子邮件内容值具有双引号或单引号时失败。如何修改我的代码以正确插入所有查询?
$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);
}
}
后来我收到了这些错误:
警告:mssql_query():message:字符串后面的未闭合引号'请调查下面的s为什么......
警告:mssql_query():常规SQL Server错误:在第38行的/var/www/testing.php中检查来自SQL Server的消息(严重级15)
答案 0 :(得分:3)
您想要使用参数:
$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);
来源:sqlsrv_query