Home > Backend Development > PHP Tutorial > How to save array into database?

How to save array into database?

WBOY
Release: 2016-09-09 08:13:14
Original
1637 people have browsed it

Let’s talk about your needs first - implement related articles based on tags and implement article indexing. There are three existing tables: article table (id, title...), tag table (tagid, tag), and tag article correspondence table (aid , tagid): Add a tag field to the article publishing page and split and store multiple tags in one article into the database. Without using explode(), mysql can successfully obtain the field information and added data information, but it cannot be used after using explode(). The field information and value are always empty. Question: How can one article achieve the effect of multiple tags and indexing? Or how can the following code be improved.
How to save array into database?
How to save array into database?

<code>$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("添加失败!");
      }
       
    }</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template