gpt4 book ai didi

php - MySQL/PHP INSERT MySQL 问题?

转载 作者:行者123 更新时间:2023-11-30 23:42:32 26 4
gpt4 key购买 nike

如何将两行插入到两个不同的表中。我好像没听懂?

这是下面的代码。

// tag is not there, so insert a new instance
$mysqli = new mysqli("localhost", "root", "", "sitename");
$clean_url = mysqli_real_escape_string($mysqli, $page);
$dbc = mysqli_query($mysqli,"INSERT INTO q_tags (tag_id, users_q_id) VALUES ('$tag_num', '$page')");
$dbc .= mysqli_query($mysqli,"INSERT INTO tags (tag) VALUES ('$tag')");

echo "$tag has been entered";

if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
}

最佳答案

. 运算符用于附加字符串。您正在尝试使用此运算符附加 bool 值(mysqli_query 的结果),这意味着当您稍后检查它的值时,它不会保留您期望的值。试试这个:

// tag is not there, so insert a new instance
$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 ('$tag_num', '$page')";
$query2 = "INSERT INTO tags (tag) VALUES ('$tag')":

if (!mysqli_query($mysqli, $query1)) {
print mysqli_error($mysqli);
return; // or exit if this isn't in a function call
}

if (!mysqli_query($mysqli, $query2)) {
print mysqli_error($mysqli);
return; // or exit if this isn't in a function call
}

echo "$tag has been entered";

关于php - MySQL/PHP INSERT MySQL 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1844735/

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