$dbc->connect_error
を使用して、自分のデータベイに接続しようとしているときにエラーが発生していないかどうかを確認しようとしています。
お知らせ:未定義のプロパティ: CでPDO :: $のconnect_error:\ xamppの\ htdocsに\ add_products_processing.phpライン7
に私はのWindows7を使用しています、私は常にエラーページの格言を取得しますXAMPP v3.2.2。完全なコードを以下に示します。ユーザー名とパスワードが正しいと確信しています。何かアドバイス?
<?php
$dsn = 'mysql:host=localhost;dbname=technoglance';
$username = 'root';
$password = 'password';
$dbc = new PDO($dsn, $username, $password);
if ($dbc->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
$main_class =filter_input(INPUT_POST, 'main_class');
$brand =filter_input(INPUT_POST, 'brand');
$model =filter_input(INPUT_POST, 'model');
$description =filter_input(INPUT_POST, 'description');
$quantity =filter_input(INPUT_POST, 'quantity');
$adding_date =filter_input(INPUT_POST, 'adding_date');
$sell_price =filter_input(INPUT_POST, 'sell_price');
$buying_price =filter_input(INPUT_POST, 'buying_price');
if(!empty($main_class)){
try{
$query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price) VALUES ('$main_class', '$brand', '$model', '$description', '$quantity', now(),'$sell_price', '$buying_price');";
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->exec($query);
echo "Thank you. The record has been sent successfully.<br><br>";
}
catch(PDOException $e){
echo $query . "<br>" . $e->getMessage()."<br><br>";
}
}
else{
echo '<h1>Please use the contact form or don\'t leave an empty field!</h1>';
}
?>
あなたは 'PDO :: $ connect_error'が存在するはずだと思いますか?ドキュメントのどこにも記載されていません。 – deceze
さらに、あなたのコードはSQLインジェクションを受けやすいです。 –