PHP / MySQL - 如何添加多个标签

时间:2009-12-06 09:35:01

标签: php mysql

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

有人可以给我一些我需要在脚本中更改的示例,以便执行此操作。

以下是我的MySQL表格。

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);
}
?>

2 个答案:

答案 0 :(得分:2)

您可以使用explode()

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

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

然后你可以循环遍历数组以插入数据库

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

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

这样你就不会有两个同名的标签。请在此处查看有关UNIQUE语法

的进一步说明

此处进行编码而不测试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)");
    }
}

答案 1 :(得分:1)

避免使用子查询mysql_insert_id()为实现相同目标提供了良好的结果。

另一件事 - $_POST['tags'] 之前检查explode()是否有逗号以确保您将获得一个数组,并检查循环是否为trim($tags[$x]) == ''(什么如果$ _POST ['tags']可能会发生,例如:$_POST['tags'] === "tag1,""tag1, "

并没有与问题本身联系,但尝试每个请求只使用一个数据库连接(只要没有充分的理由以其他方式执行)。