没有事务(php)插入不起作用

时间:2015-07-29 15:49:49

标签: php

我的代码有什么问题,它会与beginTransaction和commit一起使用但是如果我不使用它们什么也没发生(也没有错误)......

private $connection="";

public function __construct($flags=0)
{

    $dsn = 'mysql:host='.HOST.';dbname='.DATABASENAME.';charset=utf8';
    $this->connection= new PDO($dsn, USERNAME, PASSWORD);
    $this->connection->setAttribute(PDO::ATTR_ERRMODE,   PDO::ERRMODE_EXCEPTION);

} 

public function startTransaction()
{
    if($this->connection)
       $this->connection->beginTransaction();
    else
    {
        $this->errors[]='try to starttTransaction without connection';
    }

public function commit()
{

    $this->connection->commit();
}


public function insert($inputObj)
{
    if( ($this->connection) && (is_object($inputObj) ) )
    {

        $table=$inputObj->tableName;
        $parametrs=implode(',', array_fill(0,count($inputObj->parametrs),'?') );
        $fields=implode(',',$inputObj->fields);
        $sql='INSERT INTO '.$table."($fields) VALUES(".$parametrs.')';
        $stmt=$this->connection->prepare($sql);
        $stmtkeeper=$stmt->execute($inputObj->parametrs);

        if($stmtKeeper)
           {

               if($inputObj->isFlag('lastId'))
                  return $this->connection->lastInsertId();
           }


        else
        {
            $this->errors[]='insertion not match';

        }
    }
    else
    {
        $this->errors[]="insertion without connection";
    }

}
index.php中的

如果我使用startTransaction并提交它有效....

<?php
$ctn=new CONTAINER('test1',array('name','f_id'),NULL,array('try',12),array(CONTAINER::LASTID));
$db=new DB();
//$db->startTransaction();
$db->insert($ctn);
//$db->commit();
$db->disconnect();

&GT;

0 个答案:

没有答案