Home  >  Article  >  Backend Development  >  PHP 对数据库操作。一次对多表插入

PHP 对数据库操作。一次对多表插入

WBOY
WBOYOriginal
2016-06-23 14:02:071131browse

public function add($u_id, $name, $big_classes){  if(!self::$object)  {    return 102;  }  $sql = 'insert classes (cl_big_classes, cl_name) value ('.$big_classes.', \''.$name.'\')';  $this->sql->query($sql);  if($this->sql->affected_rows <= 0)  {    return 220;  }  $last_id = $this->sql->insert_id;  if($last_id)  {    $sql = 'insert u_cl_link (u_id, cl_id) value ('.$u_id.', '.$last_id.')';    //这句很明显的不是上一句的操作结果,是最上一句的结果。 最后的结果是返回0,表示一切正常,但数据库最后只插入了第一条。第二条完全没有执行过,求解决方法    if($this->sql->affected_rows <= 0)     {      return 222;    }    return 0;  }}


求解决方法,理论上说,是分别执行的两次query,第二句不会没有执行的啊。但第二句的插入操作没有成功,我把第二句$sql复制到命令行执行,可以成功插入,这里应该是有个啥机制我没理解的吧。求高人指点一二,


回复讨论(解决方案)

但是并没有看到
$sql = 'insert u_cl_link (u_id, cl_id) value ('.$u_id.', '.$last_id.')';
的 $sql 在哪里被执行啊

但是并没有看到
$sql = 'insert u_cl_link (u_id, cl_id) value ('.$u_id.', '.$last_id.')';
的 $sql 在哪里被执行啊
马上结贴,藏起来,又丢人了。我检查了半天,原来我没写
$this->sql->query($sql);
。。。。。。。。。。。。。。。。。。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn