-1

可能重复:
为什么带单引号的字符串在插入数据库时​​会引发错误?
插入包含撇号(单引号)的数据时出现 MySQL 错误?

每当我将单引号('')放在文本区域中时,我总是会收到错误消息

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test'','1351029587','10/23/2012','0','0','1492815560','0')' at line 1

我插入了一个 html 删除功能:

$title = html_remove($title);
$body = html_remove($body);

function html_remove($input) {
    $old = array('<','>');
    $new = array('&lt;','&gt;');
    $input = str_replace($old, $new, $input);
    return $input;
    }

是的,$title 和 $body 是有效的工作变量。

$body = $_POST['body'];
$title = $_POST['title'];

不确定这是否正是你这样做的方式,但这几乎就是我所得到的。

4

2 回答 2

0

您应该立即停止使用您的代码。它容易受到 SQL 注入的攻击。此外,mysql_功能正在被弃用。您应该使用带有绑定变量的准备好的语句,使用mysqli_orPDO函数。

您可能想要研究strip_tags从字符串中去除 HTML 和 PHP 标记或htmlentities将所有适用字符转换为 HTML 实体的函数。阅读此处了解差异和示例。

这是一个让您走上正确道路的示例:

<?php

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = mysqli_prepare($link, "INSERT INTO myTable (body, title) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, 'ss', $_POST[body], $_POST[title]);

/* execute prepared statement */
mysqli_stmt_execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
mysqli_stmt_close($stmt);

/* close connection */
mysqli_close($link);
?>
于 2012-10-23T22:13:29.293 回答
0

在准备好的语句中使用以下变量:

$body=htmlentities($_POST['body']);
$title=htmlentities($_POST['title']);

htmlentities()将 and 转换<>and&lt;并且&gt;执行的准备好的语句将用 a 转义'and"字符\

于 2012-10-23T22:14:53.230 回答