当有人输入标签时,我可以更新我的两个mysql表中的所有内容我只是不知道如何将标签表id值添加到q_tags表tag_id值,当用户输入标签时它们将是相同的。
我希望我能解释清楚。
有人可以帮帮我吗。
以下是MySQL表格。
CREATE TABLE q_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL, //This value should be same as the tags table id
users_q_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 q_tags.*, tags.* FROM q_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 q_tags.*, tags.* FROM q_tags INNER JOIN tags ON tags.id = q_tags.tag_id WHERE q_tags.users_q_id=3");
if(!mysqli_num_rows($dbc)){
$mysqli = new mysqli("localhost", "root", "", "sitename");
$clean_url = mysqli_real_escape_string($mysqli, $page);
$query1 = "INSERT INTO q_tags (tag_id, users_q_id) VALUES ('', '$page')";
$query2 = "INSERT INTO tags (tag) VALUES ('$tag')";
if (!mysqli_query($mysqli, $query1)) {
print mysqli_error($mysqli);
return;
}
if (!mysqli_query($mysqli, $query2)) {
print mysqli_error($mysqli);
return;
}
echo "$tag has been entered";
if (!$dbc) {
print mysqli_error($mysqli);
}
}
mysqli_close($mysqli);
}
?>
答案 0 :(得分:0)
您似乎正在尝试在q_tags表中插入null。您需要先检索最后插入的ID,然后才能将其插入q_tags表中。
请改为尝试:
$query1 = "INSERT INTO tags (tag) VALUES ('$tag')";
if (!mysqli_query($mysqli, $query1)) {
print mysqli_error($mysqli);
return;
}
$lastId = mysql_insert_id();
$query2 = "INSERT INTO q_tags (tag_id, users_q_id) VALUES ('$lastId', '$page')";
if (!mysqli_query($mysqli, $query2)) {
print mysqli_error($mysqli);
return;
}