Laravel:语法错误或访问冲突

时间:2014-03-18 10:03:37

标签: php laravel-4

尝试插入文件时出现奇怪的错误'图像'和文本字段' desc'

刀片

{{ Form::file('image') }}</br>
{{ Form::label('desc', trans('Description of the Image')) }}
{{ Form::text('desc', Input::old('desc'), array('id' => 'desc','name' => 'desc', 'class' => 'form-control')) }}
{{ Form::hidden('title-id', $title['id']) }}

控制器

  public function uploadImage()
    {
        $input = array('image' => Input::file('image'), 'desc' => Input::get('desc'), 'title-id' => Input::get('title-id'));

        $this->title->uploadImage($input);

        return Redirect::back()->withSuccess( trans('main.uploaded image success') );
    }

dbwriter所

public function uploadImage(array $input)
    {   
        $title = $this->title->find($input['title-id']);
        $name  = str_random(25);
        $insert = array('local' => asset('assets/images/'.$name.'.jpg'), 
                        'desc' => $input['desc'], 
                        'title_id' => $input['title-id']);

        $this->images->saveTitleImage($input, $name);
        $this->dbWriter->compileInsert('images', $insert)->save();

        Event::fire('Titles.Modified', array($input['title-id']));
    }

错误:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc,title_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE local = values(local), d' at line 1 (SQL: INSERT INTO images (local,desc,title_id) VALUES (/assets/images/o00U6rVZPDbkkKUHjDWajHYUO.jpg, sdsd, 1) ON DUPLICATE KEY UPDATE local = values(local), desc = values(desc), title_id = values(title_id))

1 个答案:

答案 0 :(得分:1)

您不应该使用 desc 作为列名,因为它是MySQL中的保留关键字。

要修复它,只需更改列名称(例如 description )并将$ insert更改为:

$insert = array('local' => asset('assets/images/'.$name.'.jpg'), 'description' => $input['desc'], 'title_id' => $input['title-id']);

您可以阅读有关MySQLa保留关键字here

的更多信息
相关问题