Developer Jay
Developer Jay

Reputation: 1383

Undefined variable in Laravel Insert Statement

I'm currently learning laravel, not entirely sure what the meaning of the undefined variable means within my SQL statement:

my statement is:

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';
}

error is:

ErrorException (E_NOTICE)
Undefined variable: firstname

The way I have determined the variables are below, which hopefully is the the correct way:

My Controller:

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';
}
}

Upvotes: 0

Views: 1324

Answers (3)

barcasal
barcasal

Reputation: 309

Undefined variable means the variable does not exist (a null value wouldn't be a problem as long as the statement allows nullable values). Most probably, your variable does not exist either due to scope issues, typos or something else that we can't determine from your snippet.

Besides that, your query is not correct and should be changed to the following. Moreover, instead of running a statement you could use an insert:

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

You can also use name bindings instead of the ? for readability:

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

This is also the same as above (if the table has an auto-incrementing id, this method returns it):

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

Eloquent can be used to achieve the same thing:

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

Edit: Syntax changes

Edit (per author's comment)

Since this is a public function, I suspect is also a route action. Assuming this is a POST, your route may have misconfigured parameters.

Either change your route parameters to /{firstname}/{lastname}/{email}/{telephone} in your decleration inside web/api.php or (better) inject the Illuminate\Http\Request object in the insertContact function and access the values like this: $request->input('firstname') etc...

More info is available in the official documentation for request values and route parameters.

Upvotes: 1

Cory Fail
Cory Fail

Reputation: 1090

Your issue is that you are not getting the forms Request in the controller. The Request is what grabs the POST data. Also, I suggest using eloquent.

public function insertContact (Request $request) //
{
     $model = App\InsertUser::create([
       'firstname' => $request->firstname,
       'lastname' => $request->lastname,
       'email' => $request->email,
       'telephone' => $request->telephone
    ]);
}

Upvotes: 1

Stranger
Stranger

Reputation: 134

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

Upvotes: -1

Related Questions