Php 我是否正确使用PDO来防止sql注入?
我花了最后一天的时间试图找出如何将PDO合并到我的代码中以防止sql注入。这就是我想到的。但是,每当我从浏览器提交信息时,它不会更新到我的表中,也不会显示任何错误消息。有点不对劲,但我不确定是什么。我肯定语法不是问题,因为我已经检查过多次了。我知道我的数据库可以访问,所以我认为我使用PDO的方式有问题。请帮帮我,伙计们 PSBE_登录包含访问我的数据库的所有信息Php 我是否正确使用PDO来防止sql注入?,php,mysql,security,pdo,sql-injection,Php,Mysql,Security,Pdo,Sql Injection,我花了最后一天的时间试图找出如何将PDO合并到我的代码中以防止sql注入。这就是我想到的。但是,每当我从浏览器提交信息时,它不会更新到我的表中,也不会显示任何错误消息。有点不对劲,但我不确定是什么。我肯定语法不是问题,因为我已经检查过多次了。我知道我的数据库可以访问,所以我认为我使用PDO的方式有问题。请帮帮我,伙计们 PSBE_登录包含访问我的数据库的所有信息 <?php require_once 'PSBE_LOGIN.php'; $db_server = mysql_connect(
<?php
require_once 'PSBE_LOGIN.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL:" . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database:" . mysql_error());
if (isset($_POST['title']) &&
isset($_POST['author']) &&
isset($_POST['isbn']))
//This checks to see if there is a value inputted into the form at the bottom
{
$title = get_post('title');
$author = get_post('author');
$isbn = get_post('isbn');
//This retrieves information from the user and assigns it to a variable
$stmt = $pdo->prepare('INSERT INTO classifieds(title, author, isbn)
. VALUES(:title, :author, :isbn)');
$stmt->execute(array('title'=> $title, 'author'=> $author, 'isbn' => $isbn));
}
echo <<<_END
<form action="PSBE_POST_AD.php" method="post">
Title <input type="text" name="title" />
Author <input type="text" name="author" />
ISBN <input type="text" name="isbn" />
<input type="submit" value="ADD RECORD" />
</form>
_END;
?>
编辑:重写代码以包含PDOAPI
<?php
require_once'connection.php';
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
if (isset($_POST['title']) &&
isset($_POST['author']) &&
isset($_POST['isbn']))
//This checks to see if there is a value inputted into the form at the bottom
{
$title = get_post('title');
$author = get_post('author');
$isbn = get_post('isbn');
//This retrieves information from the user and assigns it to a variable
$stmt = $pdo->prepare('INSERT INTO classifieds(title, author, isbn)
. VALUES(:title, :author, :isbn)');
$stmt->execute(array('title'=> $title, 'author'=> $author, 'isbn' => $isbn));
}
echo <<<_END
<form action="PSBE_POST_AD.php" method="post">
Title <input type="text" name="title" />
Author <input type="text" name="author" />
ISBN <input type="text" name="isbn" />
<input type="submit" value="ADD RECORD" />
</form>
_END;
function get_post($var){
return mysql_real_escape_string($_POST[$var]);
}
?>
您没有正确使用它们。您需要知道您正在使用mysql\uuAPI进行连接。否则,准备工作是正确的。将两者都去掉
$title = get_post('title');
$author = get_post('author');
$isbn = get_post('isbn');
及
因为您正在使用的函数是基于mysql_uu函数的,并且这两个API不会混合使用
您不需要它,因为您已经在使用占位符了
而将其替换为
$title = $_POST['title'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
你也需要改变
$stmt = $pdo->prepare(...
到
给定您的PDO连接$db=new PDO…您使用的是两种不同类型的MySQL API,它们不会混合使用。使用,或与一起使用。@Fred ii-I删除了前5行代码,并使用PDO API连接。连接成功,但我的其余代码消失了。我无法再使用我的表单,因为它无法显示。还有什么我遗漏的吗?看看他从中得到的,函数get_post被定义为函数get_post$var{return mysql_real_escape_string$_post[$var];}。如果不存在活动的mysql_*连接,则此操作将失败。然而,这个函数实际上是一个不好的实践,因为它可能会给人一种印象,认为SQL注入是一个输入问题。好吧,正如Gumbo所说的,并且发现Gumbo+1很好,你不能使用它。我建议您通过去掉$title=get_post'title'来调试它$author=get_post'author'$isbn=获取并发布“isbn”;并将其替换为POST变量。然后,理论上,它应该进入高速档;-这是一个mysql_uu函数,而且它们不会混合使用。另外,即使使用PDO语法,也不需要该函数;您已经在使用占位符。所以废弃函数get_post$var{return mysql_real_escape_string$_post[$var];}啊!成功了。谢谢你的帮助!
$stmt = $pdo->prepare(...
$stmt = $db->prepare(...