这是我目前的代码
$search=$_GET["Search"];
$search = addcslashes(mysql_real_escape_string($search), '%_');
此代码由 DREAMWEAVER CS4 从此处生成
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_Echos, $Echos);
查询数据库的SQL语句
$query_Recordset1 = "SELECT catelogue.ARTIST, catelogue.TITLE, catelogue.`CAT NO.`, catelogue.FORMAT, catelogue.`IMAGE PATH` FROM catelogue WHERE catelogue.TITLE LIKE '%$search%'";
更多 Dremweaver 代码
$Recordset1 = mysql_query($query_Recordset1, $Echos) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
之后数据以表格格式显示
我对这一切还是陌生的,最近几天我发现你可以将 staements 放入我的搜索词中并破坏 8 个月的数据库工作。这不像数据库没有备份,但我宁愿安全而不是抱歉。
我读过准备好的语句是阻止此 How can I prevent SQL injection in PHP? 的最佳方法.
但正如我所说,我对此很陌生,对此知之甚少。
我的代码很简单,DW 创建的代码足够安全
是否应该改变一些东西以使其更简单
如果它不够安全,我该如何让它更安全?
我在这里使用准备好的 staement 吗?如果可以,有人可以在这里解释如何使用它
如有任何帮助,我们将不胜感激
最佳答案
您的代码存在很多不良做法,但它并不像我以前看到的常见 PHP 代码片段那样完全不安全或完全糟糕。
简单来说 - 您不想使用 PHP 的 mysql_
函数。你应该做的是使用 PDO .
mysql_real_escape_string
是安全的,但不是 100%。如需更好的解释,请参阅解释 when mysql_real_escape_string is vulnerable 的链接.
因此,您要做的是使用 PDO 创建准备好的语句,然后绑定(bind)输入值。 PDO 根据正在使用的字符集和数据库清理它们,这使得它 100% 安全并且不用担心您是否遗漏了什么。
关于php - 我的代码可以避免 SQL 注入(inject)吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8742259/