PHP/MySQL - How to add multiple tags -


I have this script which allows only users to enter the same tag, but I enter more than one tag Let me do a comma, for example, a comma, shoe, shirt, hat, glasses and store each tag in the database.

Can someone give me some examples of the need to convert some scripts to me in sequence?

Below here is my MySQL table.

  Create table queries (id INT is not unlocked, ultrasonement, tag_ id INT is not unknown, users_questions_id INT is not reserved, primary key (id)); Create a table tag (id INT is not unwritten, tap AuTegrment, Tag VARCHAR tap (255), primary key (ID));  

Below is the script.

  & lt; Php require_once ('./mysqli_connect.php'); $ ($ _ POST ['Submit']) {$ mysqli = New mysqli ("localhost", "root", "", "sitename"); $ Dbc = mysqli_query ($ mysqli, "SELECT questions_tags. *, Tag.); Tag (mysqli_error ($ mysqli);} $ page = '3'; $ tag = mysqli_real_escape_string ($ mysqli, $ _POST ['tag']); $ (! $ Dbc); Mysqli = new mysqli ("localhost", "root", "", "sitename"); $ Dbc = mysqli_query ($ mysqli, "SELECT questions_tags. *, Tags. * From Question_tag Add tag to the INNER tag .id = questions_tags. Tag_id WHERE questions_tags.users_questions_id = '$ page' "); if (mysqli_num_rows ($ dbc)> = 0) {$ mysqli = new mysqli (" localhost "," root "," "," sitename "); $ Clean_url = mysqli_real_escape_string ($ mysqli, $ page); $ Query1 = "include tags ('$ tag')"; if (! Mysqli_query ($ mysqli, $ query1)) {print mysqli_error ($ mysqli); return;} $ Mysqli = new mysqli ("localhost "," Root "," "," sitename "); $ dbc = mysqli_query ($ mysqli," tags select tag where id = '$ tag'); If (! $ Dbc) {print mysqli_error ($ mysqli); } And {while ($ line = mysqli_fetch_array ($ dbc)) {$ id = $ row ["id"]; }} $ Query2 = "VALUES ('$ id', '$ page')" Enter questions in the questions (tag_ id, users_questions_id) "; If (! Mysqli_query ($ mysqli, $ query2)) {print mysqli_error ($ mysqli); Return; } "$ Tag entered" echo; If (! $ Dbc) {print mysqli_error ($ mysqli); }} Mysqli_close ($ mysqli); }? & Gt;  

you can use

to an array of tags Separate to commas by

  $ tag_string = "T1, T2, T3"; $ Tag = explosion (",", $ tag_string); $ Tag echo [0]; // t1 $ tag echo [1]; // t2  

You can then loop through the array to insert the database

You can also include UNIQUE You may also want to create your own query

  create table tag (ID INT is not unlocked, Neural AuTengment, Tag VARCHAR (255) Faucet, primary key (ID), unique (`tag`) ));  

In this way you will not tag two tags with the same name. For more explanation on syntax, see here

Here the coding goes without testing the xD

  // Assume that you already have questions And mysql_insert_Id is added () == 1 // where mysql_insert_Id () is the last id added in the query table (if isset ($ _ POST ['tags']) {$ tag = explosion (",", $ _POST [ 'Tags']); ($ X = 0; $ x & lt; count ($ Tag); $ x ++) {// Because of unique it will only be inserted if the tag dosent already exists mysql_query ("Insert in tag values ​​(zero, {$ Tags [X]}) "); // Add Relational Link mysql_query ("Enter the question in question (tap, (tag tag where tag tag =. {Tags tag [x]}), 1);); }}  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -