-5

所以我试图使用 PDO 来防止 sql 注入,但是在运行我的代码之后,数据没有输入到数据库中。如果有人能找到错误,我将不胜感激。

header("location:employees.php");

$connect = mysql_connect("localhost","root","");
if(!$connect){
    die(mysql_error($connect));
}
mysql_select_db("employees",$connect) or die(mysql_error()); 
$dbConnection = new PDO('mysql:dbname=employees;host=localhost;', 'localhost', '');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$name = $_POST['name'];
$position = $_POST['position'];
$wage = $_POST['wage'];
$preparedStatement = $db->prepare('INSERT INTO employees (name, position, wage) VALUES (:name, :position, :wage)');
$preparedStatement->execute(array(':name' => $name,':position' => $position,':wage' => $wage));
mysql_query($preparedStatement);

我没有收到任何错误或任何东西,但没有将用户输入数据库。

4

1 回答 1

2

您在这里混合概念:使用 PDO 或mysql_函数:

header("location:employees.php");

$dbConnection = new PDO('mysql:dbname=employees;host=localhost;', 'localhost', '');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$name = $_POST['name'];
$position = $_POST['position'];
$wage = $_POST['wage'];
$preparedStatement = $db->prepare('INSERT INTO employees (name, position, wage) VALUES (:name, :position, :wage)');
$preparedStatement->execute(array(
    ':name' => $name,
    ':position' => $position,
    ':wage' => $wage
));
于 2013-06-28T01:30:17.587 回答