gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:08:26 24 4
gpt4 key购买 nike

我有这个脚本,它只允许用户输入一个标签,但我想让用户输入多个用逗号分隔的标签,例如 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);
}
?>

最佳答案

你可以使用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)");
}
}

关于PHP/MySQL - 如何添加多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com