-1

I try to get rid of procedural and switch to almost full OOP. I've made an abstract class 'Vehicle', and instantiated an extended one 'Car'.

I've written an insert() method which insert datas in my database : I can't make it work !

here's the abstract class :

    <?php

abstract class Vehicle
{
    private $id;
    private $marque;
    private $model;
    private $imma;

    protected function __construct($ma, $mo, $im)
    {
        $this->marque = $ma;
        $this->model = $mo;
        $this->imma = $im;
    }

    public function setMarque($ma)
    {
        $this->marque = $ma;
    }

    public function setModel($mo)
    {
        $this->model = $mo;
    }

    public function setImma($im)
    {
        $this->imma = $im;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getMarque()
    {
        return $this->marque;
    }

    public function getModel()
    {
        return $this->model;
    }

    public function getImma()
    {
        return $this->imma;
    }

    public function insert(PDO $bdd, $idA)
    {

        try{
            $bdd->prepare('INSERT INTO vehicle (immatriculation, id_a)
                            VALUES ("'.$this->imma.'",'.$idA.')')->execute();

        }
        catch(Exception $e) {
            echo '<script>alert("An error has occured, try again...");</script>';
            return;
        }
        $this->id =
            $bdd->prepare('SELECT id_vehicle FROM vehicle 
                           WHERE immatriculation="'.$this->imma.'"')->execute();
    }


}
Aleks
  • 111
  • 2
  • 10
  • 1. You need to get an error from PDO, but NOT the way you are doing it now. Refer to the linked answer for the details. 2. You are using PDO prepared statement wrong way. [Placeholders have to be set instead of actual data in the query, while data have to go into execute](https://phpdelusions.net/pdo#prepared) – Your Common Sense Mar 29 '16 at 17:38

1 Answers1

-1

Try:

$bdd->prepare("INSERT INTO vehicle (immatriculation,id_a) VALUES ('".$this->imma."','".$idA."');")->execute();

If it does not work, Please give me the error you get.