PlayHardGoPro
PlayHardGoPro

Reputation: 2942

DB insert with laravel

I'm using ajax to update the database when I drag/drop an element from a nested list. But The query is getting the parameter I pass AS column name.

Code inside my Model:

public static function atualiza_drag($disc, $professor, $old)
    {
        DB::insert('INSERT INTO disciplina_professor (disciplina_id, professor_id) VALUES (`$disc`, `$professor`)');        
        DB::delete("DELETE FROM disciplina_professor WHERE professor_id = `$old`");
    }

The Error:

local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'field list'

3 is the ID I got from ajax but instead of using this as parameter it's using as a column name and I cant find out why.

Upvotes: 1

Views: 1150

Answers (1)

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

Please remove grave accent (`) from the variables.

Here it should looks like

public static function atualiza_drag($disc, $professor, $old)
    {
        DB::insert('INSERT INTO disciplina_professor (disciplina_id, professor_id) VALUES ($disc, $professor)');        
        DB::delete('DELETE FROM disciplina_professor WHERE professor_id = $old');
    }

Note: If you use single quote (') in your insert then do also use single quote in your delete.

Characters and symbols names
Single qoute = '
Grave accent = `

Resources
http://laravel.com/docs/5.1/database
http://www.lookuptables.com/
http://www.ascii.cl/htmlcodes.htm

Upvotes: 2

Related Questions