-5

そのため、SQL インジェクションを防ぐために PDO を使用しようとしていますが、コードを実行した後、データがデータベースに入力されません。誰かがエラーを見つけることができれば、私は非常に感謝しています.

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 に答える