使用此查询插入评论
$sql = 'INSERT INTO comments (topic_id,commenters_id,comment,media,mediaType) VALUES ("' . $topic_id . '", "' . $commenter_id . '", "' . $comment . '", "' . $media . '", "' . $mediaType . '")';
答案 0 :(得分:0)
您应该能够插入包含SQL的注释而不执行它。您遇到问题的原因是您将用户输入连接到SQL字符串中。如果您使用预准备语句并将用户输入绑定为参数,则您不会遇到此问题。这是一个如何执行此操作的示例(改编自PHP quickstart guide on mysqli prepared statements)。
$sql = 'INSERT INTO comments
(topic_id,commenters_id,comment,media,mediaType) VALUES (?, ?, ?, ?, ?)';
// prepare
if (!($stmt = $mysqli->prepare($sql))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
// bind parameters (I'm not sure if I picked the correct types here)
if (!$stmt->bind_param("iisss", $topic_id, $commenter_id, $comment, $media, $mediaType)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
// execute
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}