PHP/MySQL-如何添加多个标记
我有一个脚本,它只允许用户输入一个标签,但我想让用户输入多个用逗号分隔的标签,例如鞋子、衬衫、帽子、眼镜,并将每个标签存储在数据库中 有人能给我举几个例子,说明我需要在脚本中修改什么才能做到这一点 下面是我的MySQL表PHP/MySQL-如何添加多个标记,php,mysql,Php,Mysql,我有一个脚本,它只允许用户输入一个标签,但我想让用户输入多个用逗号分隔的标签,例如鞋子、衬衫、帽子、眼镜,并将每个标签存储在数据库中 有人能给我举几个例子,说明我需要在脚本中修改什么才能做到这一点 下面是我的MySQL表 CREATE TABLE questions_tags ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, tag_id INT UNSIGNED NOT NULL, users_questions_id INT UNSIGNED NOT NU
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);
}
?>
你可以用
获取用逗号分隔的标记数组
$tag_string = "t1, t2, t3";
$tags = explode(",", $tag_string );
echo $tags[0]; // t1
echo $tags[1]; // t2
然后可以循环遍历数组以插入到数据库中
您可能还希望您的Create查询包含UNIQUE
这样你就不会有两个同名的标签。请在此处查看有关语法的进一步解释
这里是编码而不测试xD
避免子查询,而mysql_insert_id为实现相同的目标提供了良好的结果
另一件事-在分解之前检查$\u POST['tags']是否有逗号,以确保您将获得一个数组,并且还检查循环中是否修剪$tags[$x]==如果$\u POST['tags']将发生什么,例如:$\u POST['tags']==tag1或tag1
与问题本身无关,但只要没有充分的理由,每个请求只能使用一个db连接。我真的不使用mysqli。如果我以基本方式向您展示,可以吗?现在任何东西都会有所帮助,并且很好地注意到独特性:应该可以,需要记住的是,上面的代码假设您已经添加了问题,并且您的Id值为1;但不幸的是,它并没有那么简单,您需要理解上面的代码,然后看看如何使用它
CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE(`tag`)
);
//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)");
}
}