Home >Backend Development >PHP Tutorial >How to save array into database?
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.
<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>