PHP/MySQL-如何添加多个标签 [英] PHP/MySQL - How to add multiple tags

查看:126
本文介绍了PHP/MySQL-如何添加多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,该脚本只允许用户输入一个标签,但我想让用户输入多个用逗号分隔的标签,例如shoe, shirt, hat, glasses并将每个标签存储在数据库中.

I have this script that only lets users enter a single tag but I want to let users enter multiple tags that are separated by a comma for example, shoe, shirt, hat, glasses and store each tag in the database.

有人可以给我几个例子来说明我需要在脚本中进行哪些更改才能做到这一点.

Can someone please give me a couple of examples of what I need to change in my script in-order to do this.

这是我下面的MySQL表.

Here is my MySQL tables below.

CREATE TABLE questions_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL,
users_questions_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

这是下面的脚本.

<?php 
require_once ('./mysqli_connect.php');

if (isset($_POST['submitted'])) {

        $mysqli = new mysqli("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags, tags");
    if (!$dbc) {
        print mysqli_error($mysqli);
    }

$page = '3';

$tag = mysqli_real_escape_string($mysqli, $_POST['tag']);

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags INNER JOIN tags ON tags.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 = "INSERT INTO tags (tag) VALUES ('$tag')";

if (!mysqli_query($mysqli, $query1)) {
    print mysqli_error($mysqli);
    return;
}

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='$tag'");

if (!$dbc) {
    print mysqli_error($mysqli);
}  else {
    while($row = mysqli_fetch_array($dbc)){
        $id = $row["id"];
    }
}

$query2 = "INSERT INTO questions_tags (tag_id, users_questions_id) VALUES ('$id', '$page')";

if (!mysqli_query($mysqli, $query2)) {
    print mysqli_error($mysqli);
    return;
}

echo "$tag has been entered";

    if (!$dbc) {
            print mysqli_error($mysqli);
    }
}
mysqli_close($mysqli);
}
?>

推荐答案

您可以使用爆炸()

获取由逗号分隔的标签数组

To get an array of tags seperated by commas

$tag_string = "t1, t2, t3";
$tags = explode(",", $tag_string );
echo $tags[0]; // t1
echo $tags[1]; // t2

然后您可以遍历数组以插入数据库

Then you can loop through the array to insert into the database

您可能还希望您的创建查询包含UNIQUE

You might also want your Create Query to include UNIQUE

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE(`tag`)
);

这样,您将不会有两个名称相同的标签.在此处查看有关 UNIQUE的进一步说明语法

This way you wont have two tags with the same name. Look here for further explanation on the UNIQUE syntax

此处无需测试xD即可进行编码

//Assuming you have already added the question and the mysql_insert_Id() == 1
//where mysql_insert_Id() is the last id added to the question table

if (isset($_POST['tags'])){
    $tags = explode(",", $_POST['tags']);

    for ($x = 0; $x < count($tags); $x++){

        //Due to unique it will only insert if the tag dosent already exist
        mysql_query("INSERT INTO tag VALUES(NULL, {$tags[x]})");

        //Add the relational Link
        mysql_query("INSERT INTO question_tag VALUES(NULL, (SELECT tags.Id FROM tags WHERE tags.tag = {$tags[x]}), 1)");
    }
}

这篇关于PHP/MySQL-如何添加多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆