如何把數組存進資料庫?

來源:互聯網
上載者:User

先說說自己的需求吧——根據標籤實現相關文章並實現文章索引,現有三表:文章表(id,title...),標籤表(tagid,tag),標籤文章對應關係表(aid,tagid):在文章發佈頁面添加標籤欄位並把一文章多標籤的情況拆分儲存進資料庫。在不使用explode()的情況下,mysql可以用順利擷取到欄位資訊和添加的資料資訊,但是當使用了explode()後就不可以了,欄位資訊和value一直為空白。問:可以怎麼實現一文章多標籤以及索引的效果?或者說以下代碼可以怎麼改善。

$article=I("post.post");      $article['eid']=I("post.eid");      $tags=I("post.tags");      $tags['tag']=explode(",",I("post.tag"));      $article['smeta']=json_encode($_POST['smeta']);      $article['post_content']=htmlspecialchars_decode($article['post_content']);      $result=$this->posts_model->add($article);      $result2=$this->tags_model->add($tags);      echo $this->tags_model->getLastSql();      dump($tags);      if ($result && $result2) {                $this->tag_relationships_model->add(array("aid"=>$result,"tagid"=>$result2));        foreach ($_POST['term'] as $mterm_id){          $this->term_relationships_model->add(array("term_id"=>intval($mterm_id),"object_id"=>$result));        }        $this->success("添加成功!");      } else {        $this->error("添加失敗!");      }           }
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.