在MySQL查询中具有表和列变量的PHP类

时间:2019-06-24 18:12:15

标签: php mysql oop

我正在尝试创建一个PHP类,该类允许我处理图像,而不管我正在使用的MySql表如何,因此表名和关联的列是该类中的变量。

我尝试了各种语法组合以使其起作用,但是没有一个起作用。

这是代码的主要部分:

// The class I'm working on

Class ImageUpdater{

    public $pdo;

    public function __construct($pdo){
        $this->pdo = $pdo;
    }

    public function imageUpdate($pdo, $table, $column, $id, $givenId, $imgDefault, $img, $guide){

        $pdo->beginTransaction();

        try{

            if($img != 'errorimage'){
            //Getting current image dir
            $stmt = $pdo->prepare("SELECT `$column` FROM `$table` WHERE `$id` = ?");
            $stmt->execute(array($givenId));
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            if($row[$column] != $imgDefault && $row[$column] != '' && $row[$column] != null
                && $row[$column] != 'errorimage'){
                    unlink($row[$column]);
                }

            //Update image link in database
            $sql = "UPDATE `$table` SET `$column` = `:$column`
            WHERE `$id` = `:$id`
            AND person_id = :person_id";
            $stmt = $pdo->prepare($sql);
            $stmt->execute(array(
                `:$column`=> $img,
                `:$id` => $givenId,
                ':person_id'=> $person
            ));

            $pdo->commit();

            return json_encode('upload successful');

            }else{
                return (json_encode('1'));
            }  

        }catch(Exception $e){
            $pdo->rollback();
            //Erase image file loaded in directory

            //Update image link in database to default image
            $sql = "UPDATE `$table` SET `$column` = `:$column`
            WHERE `$id` = `:$id`
            AND person_id = :person_id";
            $stmt = $pdo->prepare($sql);
            $stmt->execute(array(
                `:$column` => null,
                `:$id` => $givenId,
                ':person_id' => $person
            ));
            return (json_encode('imageerror'));
        }
    }
}

//Here is the resulting object I try to make work:

if (isset($_FILES['avatar']) && isset($_POST['edit'])){

  require '../config/pdo.php';
  require '../src/ImageHandler.php';
  require '../src/imageCrud.php';

  $db = new TourConn();
  $pdo = $db->connect();

  //Upload image to directory
  $table = 'users';
  $column = 'user_image';
  $id = 'user_id';
  $imgDefault = '../uploads/user.jpg';
  $upImgDir = '../uploads/'; //Directory for the uploaded image
  $file = $_FILES['avatar'];
  $givenId = $_POST['userId'];
  $person = $_SESSION['person_id'];

  $imgUp = new ImageHandler($file, $upImgDir); //this is another class that works fine

  $imgUp->imgUploadValidator($file);
  $img = $imgUp->imgHandling($file, $upImgDir);

//This is the object I have problems with

  $updater = new ImageUpdater($pdo);
  $updater->imageUpdate($pdo, $table, $column, $id, $givenId, $imgDefault, $img, $person);

}

在发出POST请求时,我在响应中收到以下错误消息:

<b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\8Go\src\imageCrud.php:59
Stack trace:
#0 C:\xampp\htdocs\8Go\src\imageCrud.php(59): PDOStatement-&gt;execute(Array)
#1 C:\xampp\htdocs\8Go\model\user_image_model.php(32): ImageUpdater-&gt;imageUpdate(Object(PDO), 'users', 'user_image', 'user_id', '1', '../uploads/user...', '../uploads/5d11...', '2')
#2 {main}
  thrown in <b>C:\xampp\htdocs\8Go\src\imageCrud.php</b> on line <b>59</b><br />

我检查了要传入的变量的数量,它确实与类所需的变量数量匹配,所以我一无所知。请帮助

0 个答案:

没有答案