PHP如何使用函数正确插入表?

时间:2018-05-23 19:37:45

标签: php mysql

我从在线教程中学习,但我似乎无法将我的SQL语句插入到数据库中。这是我第一次尝试功能。我认为这是"反击"在我的SQL语句中:

$sql = "INSERT INTO users (`" . implode('`, `', $keys) . "`) VALUES (".$values.")";`

无论如何,我的代码是db.php:

class DB
{
    private static $_instance = null;
    private 
            $_pdo, 
            $_query, 
            $_error = false, 
            $_results, 
            $_count = 0;
    private function __construct()
    {
        try
        {
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname='.Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
            echo 'Connected<br>';
        }
        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 = array())
    {
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql))
        {
            $r = 1;
            if (count($params))
            {
                foreach($params as $param)
                {
                    $this->_query->bindValue($r, $param);
                    $r++;
                }
            }
            if($this->_query->execute())
            {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();

                echo 'success<br>';             
            }
            else
            {
                $this->_error = true;
            }
        }
        return $this;
    }
    public function action($action, $table, $where = array())
    {
        if(count($where) === 3)
        {
            $operators = array('=', '>', '<', '<=', '>=');
            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];
            if(in_array($operator, $operators))
            {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
                if(!$this->query($sql, array($value))->error())
                {
                    return $this;
                }
            }
        }
        return false;
    }
    public function get($table, $where)
    {
        return $this->action('SELECT *', $table, $where);
    }
    public function delete($table, $where)
    {
        return $this->action('DELETE *', $table, $where);
    }
    public function insert($table, $fields = array())
    {
        if(count($fields))
        {
            $keys = array_keys($fields);
            $values = '';
            $r = 1;
            foreach($fields as $field)
            {
                $values .= '?';
                if($r < count($fields))
                {
                    $values .= ', ';
                }
                $r++;
            }
            //die($values);
            $sql = "INSERT INTO users (`" . implode('`, `', $keys) . "`) VALUES (".$values.")";
            echo $sql;
            if(!$this->query($sql, $fields)->error())
            {
                return true;
            }
        }
        return false;
    }
    public function error()
    {
        return $this->_error;
    }
    public function count()
    {
        return $this->_count;
    }
    public function results()
    {
        return $this->_results;
    }
    public function first()
    {
        return $this->results()[0];
    }
}

这是我的index.php:

$user = DB::getInstance()->insert('users', array(
'username' => 'Dale',
'password' => 'password',
'salt' => 'salt'
));
if($user){ echo 'true';}

我已经尝试删除&#34; back-ticks&#34; (这是教程中的人称之为(`)的内容,但是没有用。我试图让index.php将数据插入到我的数据库中。任何帮助将不胜感激。

0 个答案:

没有答案