簡體   English   中英

綁定參數問題

[英]Binding Parameters Issues

我查看了關於 SO 提出的所有其他問題,但沒有在我的代碼中找到問題。 我正在嘗試使用 Update() 方法更新數據庫。 我的 Insert() 方法已啟動並正在運行,但在運行代碼時收到上述錯誤。 綁定我的值時似乎是一個錯誤。 有人能給我一些建議嗎? 謝謝你。

<?php 


class DB{

    private static $_instance = null;
    private $_pdo,$_query,$_error = false, $_result, $_count = 0, $_lastInsertID = null;

    private function __construct(){

        try{
            $this->_pdo = new PDO('mysql:host='.DB_HOST.';port=3307;dbname='.DB_NAME , DB_USER, DB_PASSWORD);
        }catch(PDOException $e){
            die($e->getMessage());

        }
    }
 
    public static function getInstance(){
        if(!isset(self::$_instance)){
            self::$_instance = new DB();
        }
        return self::$_instance;
    }


    public function query($sql, $params = []){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)){     
            //binds paramaters
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
    
            if ($this->_query->execute()){
                $this->_result = $this->_query->fetchALL(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
                $this->_lastInsertID = $this->_pdo->lastInsertId();
            } else{
                $this->error = true;
            }
        }
        return $this;
    }

    public function insert($table,$fields=[]){
        $fieldString = '';
        $valueString = '';
        $values = [];
    
        foreach( $fields as $field => $value){
            $fieldString .= '`'. $field . '`,';
            $valueString .= '?,';
            $values[] = $value; 
        }
        $fieldString = rtrim($fieldString, ',');
        $valueString = rtrim($valueString, ',');
        $sql = "INSERT INTO {$table} ({$fieldString}) VALUES ({$valueString})";
        if(!$this->query($sql, $values)->error()){
            return true;
        }else{
            return false;
        }
    }

    public function update($table, $id, $fields = []){
        $fieldString = '';
        $values = [];
        foreach($fields as $field => $value){
            $fieldString .= ' ' . $field . ' = ?,';
        }
        $fieldString = trim($fieldString);
        $fieldString = rtrim($fieldString, ',');
        $sql = "UPDATE {$table} SET {$fieldString} WHERE id = {$id}";
        $obj = $this->query($sql,$values);
        dnd($obj);
        if(!$this->_query($sql,$values)->error()){
            return true;
        }
        return false;
    }

    public function error(){

        return $this->_error;
    }

}
?>

<?php 

class Home extends Controller{
    
    public function __construct($controller,$action){
        parent::__construct($controller, $action);
    }

    public function indexAction(){
        //die('welcome to the home controller this is the index action.');
        $db = DB::getInstance();
        $fields = [
            'fname'=> 'Jared',
            'email'=>'JBowser@123.com'];

            
        //$contacts = $db->insert('contacts',$fields);  This is how we insert to our DB. 
        $contacts = $db->update('contacts',3, $fields); // This is how we update our DB.

        $this->view->render('home/index'); ///path from views directory **
    }

}

您不會在 Update 方法中加載值數組

public function update($table, $id, $fields = []){
    $fieldString = '';
    $values = [];
    foreach($fields as $field => $value){
        $fieldString .= ' ' . $field . ' = ?,';
        $values[] = $value;                         // <<-- Added this line
    }
    $fieldString = trim($fieldString);
    $fieldString = rtrim($fieldString, ',');
    $sql = "UPDATE {$table} SET {$fieldString} WHERE id = {$id}";
    $obj = $this->query($sql,$values);
    dnd($obj);
    if(!$this->_query($sql,$values)->error()){
        return true;
    }
    return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM