Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class fooObject {
- private $dbConnexion;
- private $tableName = "my_table";
- public $foo;
- public $foo2;
- public function __construct($db) {
- $this->dbConnexion = $db;
- }
- function create() {
- try {
- $sql = "INSERT INTO " . $this->tableName . " SET foo = :foo, foo2 = :foo2";
- $query = $this->dbConnexion->prepare($sql);
- $query->bindParam(":foo", $this->foo, PDO::PARAM_STR);
- $query->bindParam(":foo2", $this->foo2, PDO::PARAM_STR);
- $query->execute();
- } catch (PDOException $e){
- echo 'PDOException : ' . $e->getMessage();
- }
- }
- }
- class DbConnect
- {
- private $host = "localhost";
- private $db_name = "base";
- private $username = "root";
- private $password = "";
- public $dbConnexion;
- // get the database connection
- public function getConnection(){
- $this->dbConnexion = null;
- try{
- $this->dbConnexion = new PDO("mysql:host={$this->host};dbname={$this->db_name}", $this->username, $this->password);
- $this->dbConnexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $e){
- echo "Connection error: " . $e->getMessage();
- }
- return $this->dbConnexion;
- }
- }
- date_default_timezone_set('Europe/Paris');
- $dataBase = new DbConnect();
- $db = $dataBase->getConnection();
- $object = new fooObject($db);
- if (isset($_POST['saveToDb'])) {
- print_r($_REQUEST);
- $object->foo = $_POST['foo'];
- $object->foo2 = $_POST['foo2'];
- $object->create();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement