Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: iNs8T
- * Date: 06/02/16
- * Time: 00:39
- */
- class CRUD_Model
- {
- private $pdocon;
- private $dsn, $username, $password;
- private $link;
- function __construct()
- {
- /*$this->dsn = 'mysql:host=127.0.0.1:3306;dbname=products';
- $this->username = 'root';
- $this->password = '';*/
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- }
- private function openConnection()
- {
- try {
- if ($this->pdocon == NULL) {
- $this->pdocon = new PDO($this->dsn, $this->username, $this->password);
- }
- }catch (PDOException $pdoexp) {
- $this->pdocon = NULL;
- throw new Exception('Database error');
- }
- }
- /* public function addItem() {
- try {
- $this->openConnection();
- $pdoStatement = $pdocon->prepare('INSERT INTO products (description, price, added_At, color, size, imagepath, gender, category, quantity, name) VALUES(:description, :price, :added_At, :color, :size, :imagepath, :gender, :category, :quantity, :name)');
- $pdoStatement->bindParam(':description', $_POST['description']);
- $pdoStatement->bindParam(':price', $_POST['price']);
- $pdoStatement->bindParam(':added_At', $_POST['added_At']);
- $pdoStatement->bindParam(':color', $_POST['color']);
- $pdoStatement->bindParam(':size', $_POST['size']);
- $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
- $pdoStatement->bindParam(':gender', $_POST['gender']);
- $pdoStatement->bindParam(':category', $_POST['category']);
- $pdoStatement->bindParam(':quantity', $_POST['quantity']);
- $pdoStatement->bindParam(':name', $_POST['name']);
- $pdoStatement->execute();
- $this->pdocon = NULL;
- } catch (PDOException $pdoexp) {
- $this->pdocon = NULL;
- throw new Exception('Database error');
- }
- } */
- public function addItem() {
- try {
- $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
- $this->username = 'kayjays_se';
- $this->password = 'hej123';
- //this->openConnection();
- $pdocon = new PDO($this->dsn, $this->username, $this->password);
- //Preparar ett statement som kommer lägga till i tabellen products
- $pdoStatement = $pdocon->prepare('INSERT INTO products (name, category,
- imagepath, size, price, gender) VALUES(:name, :category, :imagepath, :size, :price, :gender)');
- //binder ihop $_Post värdena med en parameter
- $pdoStatement->bindParam(':name', $_POST['name']);
- $pdoStatement->bindParam(':category' ,$_POST['category']);
- $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
- $pdoStatement->bindParam('size', $_POST['size']);
- $pdoStatement->bindParam(':price', $_POST['price']);
- $pdoStatement->bindParam(':gender', $_POST['gender']);
- //kör vårt tidigare angivna statement
- $pdoStatement->execute();
- $this->pdocon = NULL;
- } catch (PDOException $pdoexp) {
- $this->pdocon = NULL;
- throw new Exception('Database error');
- }
- }
- //Funktion för att radera ett item ur databasen
- public function deleteItem($id){
- try{
- $this->openConnection();
- //preparerar ett sqlstatement
- $pdoStatement = $this->pdocon->prepare('DELETE FROM products WHERE id=:id');
- $pdoStatement->bindParam(':id', $id);
- //kör vårt tidigare angivna statement
- $pdoStatement->execute();
- $this->pdocon = NULL;
- } catch (Exception $ex) {
- $this->pdocon = NULL;
- throw new Exception('Database error');
- }
- }
- //funktion för att uppdatera ett item i shopen
- public function updateItem() {
- try {
- $this->openConnection();
- //updaterar produkten i databasen där vi hittar matchning på ID
- $pdoStatement = $this->pdocon->prepare('UPDATE `products` SET `description`=:description,`price`=:price,`added_At`=:added_At,`color`=:color,`size`=:size,`imagepath`=:imagepath,`gender`=:gender,`category`=:category,`quantity`=:quantity,`name`=:name WHERE `id`=:id');
- //binder ihop datat från de olika $_post
- $pdoStatement->bindParam(':description', $_POST['description']);
- $pdoStatement->bindParam(':price', $_POST['price']);
- $pdoStatement->bindParam(':added_At', $_POST['added_At']);
- $pdoStatement->bindParam(':color', $_POST['color']);
- $pdoStatement->bindParam(':size', $_POST['size']);
- $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
- $pdoStatement->bindParam(':gender', $_POST['gender']);
- $pdoStatement->bindParam(':category', $_POST['category']);
- $pdoStatement->bindParam(':quantity', $_POST['quantity']);
- $pdoStatement->bindParam(':name', $_POST['name']);
- $pdoStatement->bindParam(':id', $_POST['id']);
- //kör vårt tidigare angivna statement
- $pdoStatement->execute();
- $this->pdocon = NULL;
- } catch (PDOException $pdoexp) {
- $this->pdocon = NULL;
- throw new Exception('Databaseerror');
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment