#1 syber
原代码如下:public function create($row)
{
if(!is_array($row))return FALSE;
$row = $this->__prepera_format($row);
if(empty($row))return FALSE;
foreach($row as $key => $value){
$cols[] = $key;
$vals[] = $this->escape($value);
}
$col = join(',', $cols);
$val = join(',', $vals);
$sql = "INSERT INTO {$this->tbl_name} ({$col}) VALUES ({$val})";
if( FALSE != $this->_db->exec($sql) ){ // 获取当前新增的ID
if( $newinserid = $this->_db->newinsertid() ){
return $newinserid;
}else{
return array_pop( $this->find($row, "{$this->pk} DESC",$this->pk) );
}
}
return FALSE;
}
如果表没有设置pk的话(允许表没有pk),这里用create就会出错,具体是:
return array_pop( $this->find($row, "{$this->pk} DESC",$this->pk) );
因为没有设置pk,所以,这里的sql会构造出select * from xxx order by desc,造成语法错误。
修复:
public function create($row)
{
if(!is_array($row))return FALSE;
$row = $this->__prepera_format($row);
if(empty($row))return FALSE;
foreach($row as $key => $value){
$cols[] = $key;
$vals[] = $this->escape($value);
}
$col = join(',', $cols);
$val = join(',', $vals);
$sql = "INSERT INTO {$this->tbl_name} ({$col}) VALUES ({$val})";
if( FALSE != $this->_db->exec($sql) ){ // 获取当前新增的ID
if( $newinserid = $this->_db->newinsertid() ){
return $newinserid;
}else{
if ($this->pk)
return array_pop( $this->find($row, "{$this->pk} DESC",$this->pk) );
}
}
return FALSE;
}
加上一句:if ($this->pk)即可。
2011-08-15 11:19:16