如果插入用户输入而不修改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)
可以采取哪些措施来防止这种情况发生?
嗨,我正在尝试学习使用预准备语句的正确方法,以避免SQL注入等.
当我执行脚本时,我从我的脚本中得到一条消息,说0行已插入,我希望这可以说1行插入,当然更新表.我对我准备好的陈述并不完全确定,因为我做了一些研究,我的意思是它因例子而异.
当我更新我的表时,我需要声明所有字段还是只更新一个字段?
任何信息都会非常有用.
的index.php
<div id="status"></div>
<div id="maincontent">
<?php //get data from database.
require("classes/class.Scripts.inc");
$insert = new Scripts();
$insert->read();
$insert->update();?>
<form action="index2.php" enctype="multipart/form-data" method="post" name="update" id="update">
<textarea name="content" id="content" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>
<input type="submit" id="update" name="update" value="update" />
</div>
Run Code Online (Sandbox Code Playgroud)
类/ class.Scripts.inc
public function update() {
if (isset($_POST['update'])) {
$stmt = $this->mysqli->prepare("UPDATE datadump SET content=? WHERE id=?");
$id = 1;
/* Bind our params */
$stmt->bind_param('is', $id, $content);
/* Set our params */
$content = isset($_POST['content']) ? $this->mysqli->real_escape_string($_POST['content']) …Run Code Online (Sandbox Code Playgroud) 为什么在PHP中启用magic_quotes_gpc被认为是一种不好的做法?