Php 使用'';
可能重复:Php 使用'';,php,Php,可能重复: 每当我在文本区域中加单引号(“”)时,我总是得到错误 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行“test”、“1351029587”、“10/23/2012”、“0”、“0”、“1492815560”、“0”)附近使用的正确语法 我插入了一个html删除函数: $title = html_remove($title); $body = html_remove($body); function html_remove($input) {
每当我在文本区域中加单引号(“”)时,我总是得到错误
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行“test”、“1351029587”、“10/23/2012”、“0”、“0”、“1492815560”、“0”)附近使用的正确语法
我插入了一个html删除函数:
$title = html_remove($title);
$body = html_remove($body);
function html_remove($input) {
$old = array('<','>');
$new = array('<','>');
$input = str_replace($old, $new, $input);
return $input;
}
我不确定你是不是就是这样做的,但这几乎就是我得到的所有信息。你应该立即停止使用你的代码。它容易受到SQL注入的攻击。此外,
mysql\uuz
函数正在运行。您应该使用mysqli\ucode>或PDO
函数将准备好的语句与绑定变量一起使用
您可能需要查看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);
?>
在准备好的语句中使用以下变量:
$body=htmlentities($_POST['body']);
$title=htmlentities($_POST['title']);
htmlentities()
将
转换为
和
,执行的预处理语句将转义'
和“
字符与\
这是一种SQL注入:修复它,你就可以摆脱HTML移除程序。当PHP有htmlspecialchars()和htmlentities()为你做这类事情时,你为什么要用str_替换东西呢?我强烈建议确保你的SQL是使用参数化查询构造的。请注意,您不希望被利用。或和的可能重复
$body=htmlentities($_POST['body']);
$title=htmlentities($_POST['title']);