gpt4 book ai didi

php - 如何将 PHP 变量传递给 Laravel DB::未准备好的查询

转载 作者:行者123 更新时间:2023-11-29 03:23:05 25 4
gpt4 key购买 nike

使用 Laravel 5.1。我在下面有这个查询。它在 phpMyAdmin 中运行良好。如果我对 $category$value 进行硬编码,它也会起作用。但是,没有多少变量的连接和/或转义可以使查询在我的 Controller 中运行。我正在使用 use Illuminate\Support\Facades\DB; 我尝试用单引号和双引号封装查询,但没有效果。我正在使用 PHPStorm,它甚至说查询是正确的,但事实并非如此。我究竟做错了什么 ??

public function addNode(Request $request)
{

$category = $request->input('parent_category');
$value = $request->input('new_category');


$result = DB::unprepared("
LOCK TABLE nested_categories WRITE;
SELECT @myLeft := lft FROM nested_categories
WHERE name = '.\'$category\'.';
UPDATE nested_categories SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_categories SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO nested_categories(name, lft, rgt) VALUES( '.\'$value\'.', @myLeft + 1, @myLeft + 2);
UNLOCK TABLES;");

return redirect('nested_categories');
}

谢谢!!

最佳答案

知道怎么做就很容易。忘记连接或反斜杠。只需使用这个:

$category = $request->input('parent_category');
$value = $request->input('new_category');

$result = DB::unprepared("
LOCK TABLE nested_categories WRITE;
SELECT @myLeft := lft FROM nested_categories
WHERE name = '$category';
UPDATE nested_categories SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_categories SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO nested_categories(name, lft, rgt) VALUES( '$value', @myLeft + 1, @myLeft + 2);
UNLOCK TABLES;");

关于php - 如何将 PHP 变量传递给 Laravel DB::未准备好的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731548/

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