我已准备好要防止SQL注入的语句,定义为:
if($stmt1 = $db->prepare("INSERT INTO `group_messages`(`group_message_sync_id`, `from_user_sync_id`, `to_user_sync_id`, `to_group_sync_id`, `sent_dt`, `read`, `read_dt`, `message_text`, `shared_campaign_id`, `shared_campaign_location_id`)
SELECT
(SELECT
CASE WHEN
max(group_message_sync_id) is null
then 0
else max(group_message_sync_id)
end + 1
from group_messages)
, ?
, b.user_sync_id
, b.group_sync_id
, NOW()
, 0
, 0
, ?
, ?
, ?
from users_groups b
WHERE
b.status = 1
and b.group_sync_id = ?
and b.user_sync_id != '?;"))
{
$stmt1->bind_params("ssssss",$userId,$message,$campaign,$location,$toGroupId,$userId);
$stmt1->execute();
$stmt1->close();
如您所见,我将6个参数传递给该语句
但是,围绕此语句的if-else子句每次都会失败。
我该如何工作?
您查询中的错误是您输入了'
,b.user_sync_id != '?;
您必须将其更改为:b.user_sync_id != ?;
关于您的有关报告错误的评论中的问题,最好的方法是使用异常:
$db = new PDO('<params>');
//this will make PDO throws exceptions in case of errors
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
$stmt1 = $db->prepare('<your query>');
$stmt1->execute();
}
catch (PDOException $e)
{
//here you handle the exception, i.e die and show the message
die( PDO Exception: ' . $e->getMessage() );
}
这样,当发生语法错误时,PDO将启动PDOException
您的catch
语句捕获的a 。例如,您可以在语句内部打印异常消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句