Undefined property: PDO::$connect_error

后端 未结 3 1090
孤城傲影
孤城傲影 2021-01-26 08:10

I am trying to use $dbc->connect_error to check if any error occurs while trying to connect to my databease. I always get an error page saying:

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 08:54

    Here is what your code should be

    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // filtering omitted
    
    if(!empty($main_class)){
        $query = "INSERT INTO products (main_class, brand, model, description, quantity, adding_date, sell_price, buying_price ) VALUES (?,?,?,?,?,?,?,?);";
        $data = [$main_class,$brand,$model,$description,$quantity,$adding_date,$sell_price,$buying_price];
        $dbc->prepare($query)->execute($data);
        echo "Thank you. The record has been sent successfully.

    ";} else{ echo '

    Please use the contact form or don\'t leave an empty field!

    '; }

    PDO will report it's errors already, without any extra code required.
    and you should be using prepared statements

提交回复
热议问题