0

i want to add an entrance (using a form) into a Mysql table using POO. Here is what i did : My class :

<?php
class portfolio{
    public $title;
    public $img_desk;
    public $img_tablet;
    public $img_phone;
    public $id;

    public function __construct($title, $img_desk, $img_tablet, $img_phone, $port_id){
        $this->title = $title;
        $this->img_desk = $img_desk;
        $this->img_tablet = $img_tablet;
        $this->img_phone = $img_phone;
        $this->id = $port_id;
    }

    public function getPortfolio($id){
        $reponse = $bdd->query('SELECT * FROM designer WHERE id = $id');
        $portfolio = $reponse->fetch();
        $a = new portfolio($portfolio['title'], $portfolio['img_desk'], $portfolio['img_tablet'], $portfolio['phone'], $portfolio['id']);
        return $a;
    }

    public function addPortfolio($title, $img_desk, $img_tablet, $img_phone){
        $add = $bdd->exec('INSERT INTO designer(title, img_desk, img_tablet, img_phone) VALUES(:title, :img_desk, :img_tablet, :img_phone)');
        $add->execute(array(  // _> was used.!!!
            'title' => $title,
            'img_desk' => $img_desk['name'],
            'img_tablet' => $img_tablet['name'],
            'img_phone' => $img_phone['name']
            // imgs...
        ));
        if (isset($img_desk) AND $img_desk['error'] == 0){
                // Testons si le fichier n'est pas trop gros
                if ($img_desk['size'] <= 3000000)
                {
                        require("class/img.class.php");
                        // Testons si l'extension est autorisée
                        $infosfichier = pathinfo($img_desk['name']);
                        $extension_upload = $infosfichier['extension'];
                        $extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
                        if (in_array($extension_upload, $extensions_autorisees))
                        {
                                // On peut valider le fichier et le stocker définitivement
                                move_uploaded_file($img_desk['tmp_name'], 'img/port/' . basename($img_desk['name']));
                        }   
                }
        }
        echo 'Élement ajouté';
    }

    public function editPortfolio($old, $new){
        $edit = $bdd->prepare('UPDATE designer SET title = :title, img_desk = :img_desk, img_tablet = :img_tablet, img_phone = :img_phone WHERE id = $old->id ');
        $edit->execute(array(
            'title' => $new->title,
            'img_desk' => $new->img_desk,
            'img_tablet' => $new->img_tablet,
            'img_phone' => $new->img_phone
            ));
    }

    public function deletePortfolio($cible){
        //DELETE FROM designer WHERE id = $cible->id;
    }

    public function getAllPortfolio(){
        //$reponse = $bdd->query('SELECT * FROM designer');
        //$donnees = $reponse->fetch();
        //return $donnees;
    }
}
?>

And here is my using on a php file containing the form :

<?php

include('class/portfolio.class.php');

        if(isset($_POST['title'])){
            addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
            echo 'ok';

        }else{
            ?>
            <form method="post" action="admin.php" enctype="multipart/form-data">
                <input type="text" placeholder="Titre" name="title"><br>
                <input type="file" name="img_desk"><br>
                <input type="file" name="img_tablet"><br>
                <input type="file" name="img_phone"><br>
                <input type="submit" value="Ajouter" name="ok">
            </form>
            <?php
        }
        ?>

And it's telling me :

Fatal error: Call to undefined function addPortfolio() in C:\wamp\www\designers\admin.php on line 47

Would someone explain me please ? I know i'm doing things wrong but i'm actually learning OOP. Thank you in advance : ).

MixedVeg
  • 319
  • 2
  • 15
  • First check your include file pass is correct or not. Second thing after including of file. you need to create the object of the class like that way. $xyz = new portfolio(); Now all the function defined in this class will refer with this object like that way. $xyz->addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']); I thing this will resolve your issue. – Lakhan Nov 24 '14 at 06:09
  • so yeah, to learn oop you could read a tutorial. reading a tutorial could have hinted you that to use a class functions, you must use this syntax: `$class->addPortFolio()` as otherwise, php is searching for the function `addPortFolio()`, not for the member function `portfolio::addPortFolio()`. if this sounds like chinese, read a tutorial and php's manual, especially the great section about oop – Félix Adriyel Gagnon-Grenier Nov 24 '14 at 20:56
  • here, read that, it answers your question, and many others you will have if you continue not reading the most basic tutorials. http://www.tutorialspoint.com/php/php_object_oriented.htm especially the section about `calling member functions` – Félix Adriyel Gagnon-Grenier Nov 24 '14 at 21:03
  • possible duplicate of ["call to undefined function" error when calling class method](http://stackoverflow.com/questions/4809702/call-to-undefined-function-error-when-calling-class-method) – Félix Adriyel Gagnon-Grenier Nov 24 '14 at 21:07

2 Answers2

1

You need to create an object first of the class portfolio

So, the corrected code should be:

// YOUR CODE HERE.

$portfolio = new portfolio;
if(isset($_POST['title'])){
  $portfolio->addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
  echo 'ok';
}else{

// YOUR CODE HERE

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Or you can directly use the function like this provided the function is declared Static but since you have personal data so not recommended!

portfolio::addPortfolio();
MixedVeg
  • 319
  • 2
  • 15