[英]Storing multiple tags in a MySQL database problem using 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),
UNIQUE('tag')
);
這是腳本。
<?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=3");
if(mysqli_num_rows($dbc) >= 0){
if (isset($_POST['tag'])){
$tags = explode(",", $_POST['tag']);
for ($x = 0; $x < count($tags); $x++){
$mysqli = new mysqli("localhost", "root", "", "sitename");
$clean_url = mysqli_real_escape_string($mysqli, $page);
$query1 = "INSERT INTO tags (tag) VALUES ('$tags[x]')";
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='$tags[x]'");
}
}
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()以分隔輸入的標簽列表。 使用foreach / for循環遍歷列表,並分別插入每個標簽。
最好的祝願,
法比安
替換為:
$query1 = "INSERT INTO tags (tag) VALUES ('$tags[x]')";
有了這個:
$query1 = "INSERT INTO tags (tag) VALUES ('" . $tags[$x] . "')";
更新 :您還需要替換以下內容:
$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='$tags[x]'");
有了這個
$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='" . $tags[$x] . "'");
x
是一個變量...必須使用$x
Update2 : questions_tags
代碼必須在for循環內
這是固定代碼
<?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=3");
if(mysqli_num_rows($dbc) >= 0){
if (isset($_POST['tag'])) {
$tags = explode(",", $_POST['tag']);
for ($x = 0; $x < count($tags); $x++){
$mysqli = new mysqli("localhost", "root", "", "sitename");
$clean_url = mysqli_real_escape_string($mysqli, $page);
$query1 = "INSERT INTO tags (tag) VALUES ('" . $tags[$x] . "')";
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='" . $tags[$x] . "'");
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";
}
}
// is this needed?
// if (!$dbc) {
// print mysqli_error($mysqli);
// }
}
mysqli_close($mysqli);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.