Laravel插入语句中的未定义变量

时间:2018-07-11 14:31:45

标签: sql laravel

我目前正在学习laravel,不确定我的SQL语句中未定义变量的含义是什么

我的陈述是:

DB::statement('INSERT INTO insert_user (firstname, lastname, email, telephone) VALUES (?)', [$firstname], [$lastname], [$email], [$telephone]);
            echo 'We will be in touch when the project is underway';
}

错误是:

ErrorException (E_NOTICE)
Undefined variable: firstname

我确定变量的方法如下,希望这是正确的方法:

我的控制器:

class ContactController extends Controller
{
//Access Modifiers & Properties

    private $firstname;
    private $lastname;
    private $email;
    private $telephone;

    public function index()
    {
        return view('contact');
    }

//Access Modifiers & Properties

    private $firstname;
    private $lastname;
    private $email;
    private $telephone;

    public function index()
    {
        return view('contact');
    }

//Insert Methods

public function insertContact ($firstname, $lastname, $email, $telephone) //
{
   $this->firstname = $firstname;
    $this->lastname = $lastname;
    $this->email = $email;
    $this->telephone = $telephone;

        //$contact = new ContactController;
            //DB::insert('INSERT INTO insert_user (firstname, lastname, email, telephone) VALUES (:firstname, :lastname, :email, :telephone)', ['firstname' => $firstname,'lastname' => $lastname,'email' => $email, 'telephone' => $telephone]);
            DB::statement('INSERT INTO insert_user (firstname, lastname, email, telephone) VALUES (:firstname, :lastname, :email, :telephone)', ['firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'telephone' => $telephone]);
            echo 'We will be in touch when the project is underway';
}
}

3 个答案:

答案 0 :(得分:1)

未定义的变量表示该变量不存在(只要语句允许可为空的值,则null值将不是问题)。您的变量很可能由于范围问题,错别字或我们无法从您的代码段中确定的其他原因而不存在。

此外,您的查询不正确,应更改为以下内容。此外,您可以使用statement来代替运行insert

DB::insert('INSERT INTO insert_user (firstname, lastname, email, telephone) 
    VALUES (?, ?, ?, ?)', [$firstname, $lastname, $email, $telephone]);

为了可读性,您也可以使用名称绑定代替?

DB::insert('INSERT INTO insert_user (firstname, lastname, email, telephone) 
    VALUES (:firstname, :lastname, :email, :telephone)', ['firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'telephone' => $telephone]);

这与上面的相同(如果表具有自动递增的ID,则此方法将其返回):

$id = DB::table('insert_user')->insert([
    'firstname' => $firstname,
    'lastname' => $lastname,
    'email' => $email,
    'telephone' => $telephone
]);

口才可以用来实现相同的目的:

$model = App\InsertUser::create([
    'firstname' => $firstname,
    'lastname' => $lastname,
    'email' => $email,
    'telephone' => $telephone
]);

编辑:语法更改

编辑(根据作者的评论)

由于这是一项公共职能,因此我怀疑这也是一项路线行动。假设这是POST,则您的路由可能配置了错误的参数。

/{firstname}/{lastname}/{email}/{telephone}内的清理中将路由参数更改为web/api.php,或者(更好)在Illuminate\Http\Request函数中注入insertContact对象并访问如下值: $request->input('firstname')等...

request valuesroute parameters的官方文档中提供了更多信息。

答案 1 :(得分:1)

您的问题是您没有在控制器中获得表单RStudioinstall.packages("lavaan") 捕获POST数据。另外,我建议使用雄辩。

Request

答案 2 :(得分:-1)

尝试一下 DB::statement('INSERT INTO insert_user (firstname, lastname, email, telephone) VALUES (?, ?, ?, ?)', [$firstname, $lastname, $email, $telephone]);