I don't know how to explain it, maybe this is understandable. I have 2 table:
- Table tags: (tagID: '1', '2', '3') (tagName: 'a', 'b', 'b')
- Table tag_posts: (postID: '1', '1', '2') (tagID: '1', '2', '3')
Output: Post 1 tagged (a[id=1], b[id=2]) and post 2 tagged (b[id=3])
But, i want result like this:
Table tags: (tagID: '1', '2') (TagName: 'a', 'b')
Table tag_posts: (postID: '1', '1', '2') (tagID: '1', '2', '2')
Output: Post 1 tagged (a[id=1], b[id=2]) and post 2 tagged (b[id=2])
I want on table "tags" tag name, b is 2; not b = 2 and b = 3 (not double tag name)
Php
$last_post_id = mysqli_insert_id($connect);
foreach($tags as $tag){
$sql = mysqli_query($connect, "SELECT * FROM tb_tags WHERE tagName = '$tag'") or die(mysqli_error());
$row = mysqli_fetch_array($sql);
if($row == NULL){
mysqli_query($connect, "INSERT INTO tb_tags (tagName) VALUES ('$tag')") or die(mysqli_error());
$last_tag_id = mysqli_insert_id($connect);
mysqli_query($connect, "INSERT INTO tb_tag_posts (postID, tagID) VALUES ('$last_post_id', '$last_tag_id')") or die(mysqli_error());
}else{
// if tags name exists do nothing and take the existing id tag
mysqli_query($connect, "INSERT INTO tb_tag_posts (postID, tagID) VALUES ('$last_post_id', '".$row['tagID']."')") or die(mysqli_error());
}
}