Self Coder
Self Coder

Reputation: 33

PHP MYSql: UPDATE Multi Row With One id

I Fetch this data from Tags Table with comma separated :

trance, house, electronica, dubstep, club

Tags Table Is:

ID        NAME             ArticleId
1         trance              10
2         house               10 
3         electronica         10
4         dubstep             10
5         garage              10

Now I need To UPDATE Tags Table With This New Input For Edit Page:

trance, house, electronica  // I remove dubstep, club tag

My PHP Code:

$arr_tag = explode(",",$tag); 
   foreach($arr_tag as $tags){
      DataAccess::put("UPDATE tags SET name = ?, type=? WHERE article = 10",$tags,"news");
   }

Now After UPDATE Ouput Is:

trance, trance, trance

My Problem Is: How To UPDATE Tags Table For New Tag Input?

Upvotes: 0

Views: 78

Answers (2)

Fernando Coronato
Fernando Coronato

Reputation: 56

You have to delete all informations first:

query = "DELETE FROM tags WHERE article = 10"

Then you save all rows again. Very easy

Upvotes: 1

Rafael
Rafael

Reputation: 2817

The problem is that your update query is replacing any rows who its article is 10 with whatever tag is the current one in the foreach loop, and the end result is that you're replacing them with the last value of your tags array. I recommend you to first delete all the relevant rows then insert them again with the information you want, but in top of all, I recommend you to read more about PHP and SQL queries in general, as you seems to be lacking in both.

Upvotes: 1

Related Questions