如果插入用户输入而不修改SQL查询,则应用程序容易受到SQL注入的攻击,如下例所示:
$unsafe_variable = $_POST['user_input'];
mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");
Run Code Online (Sandbox Code Playgroud)
这是因为用户可以输入类似的内容value'); DROP TABLE table;--,查询变为:
INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')
Run Code Online (Sandbox Code Playgroud)
可以采取哪些措施来防止这种情况发生?
我的PHP代码是:
$query="select company_name from company_names where cik=".$cik;
Run Code Online (Sandbox Code Playgroud)
哪个在印刷上给出
select company_name from company_names where cik=0001001871
Run Code Online (Sandbox Code Playgroud)
我想得到输出
select company_name from company_names where cik='0001001871'
Run Code Online (Sandbox Code Playgroud)
如何在PHP中添加单引号?