PHP 對資料庫操作。一次對多表插入

來源:互聯網
上載者:User
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);
。。。。。。。。。。。。。。。。。。。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.