Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. class fooObject {
  4.  
  5. private $dbConnexion;
  6. private $tableName = "my_table";
  7.  
  8. public $foo;
  9. public $foo2;
  10.  
  11. public function __construct($db) {
  12. $this->dbConnexion = $db;
  13. }
  14.  
  15. function create() {
  16. try {
  17. $sql = "INSERT INTO " . $this->tableName . " SET foo = :foo, foo2 = :foo2";
  18.  
  19. $query = $this->dbConnexion->prepare($sql);
  20. $query->bindParam(":foo", $this->foo, PDO::PARAM_STR);
  21. $query->bindParam(":foo2", $this->foo2, PDO::PARAM_STR);
  22. $query->execute();
  23. } catch (PDOException $e){
  24. echo 'PDOException : ' . $e->getMessage();
  25. }
  26. }
  27. }
  28.  
  29. class DbConnect
  30. {
  31. private $host = "localhost";
  32. private $db_name = "base";
  33. private $username = "root";
  34. private $password = "";
  35. public $dbConnexion;
  36.  
  37. // get the database connection
  38. public function getConnection(){
  39. $this->dbConnexion = null;
  40. try{
  41. $this->dbConnexion = new PDO("mysql:host={$this->host};dbname={$this->db_name}", $this->username, $this->password);
  42. $this->dbConnexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  43. }catch(PDOException $e){
  44. echo "Connection error: " . $e->getMessage();
  45. }
  46. return $this->dbConnexion;
  47. }
  48. }
  49.  
  50. date_default_timezone_set('Europe/Paris');
  51.  
  52. $dataBase = new DbConnect();
  53. $db = $dataBase->getConnection();
  54.  
  55. $object = new fooObject($db);
  56.  
  57. if (isset($_POST['saveToDb'])) {
  58. print_r($_REQUEST);
  59. $object->foo = $_POST['foo'];
  60. $object->foo2 = $_POST['foo2'];
  61.  
  62. $object->create();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement