duanchao1002 2017-11-10 16:35
浏览 141
已采纳

如何使用DB :: insert作为预处理语句LARAVEL

I am trying to make my insert query protected against sql injection. But I am having issues getting this to work. any ideas? I have tried several things.

 $bullets = Input::get('bullet_content');
        $product_id = Input::get('product_id');
        $user_id = Input::get('user_id');
        $retailer_id = Input::get('retailer_id');
        $date = date("Y-m-d H:i:s");
        foreach ($bullets as $bullet){


            $query = "'INSERT INTO bullets(product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) 
                        VALUES('?','?','?','?','?','?')', [$product_id,$user_id,$bullet,'N',$date,$date]";


                        DB::insert($query);
        }
        return back()->with('message','Features add successfully!');

When I try this I get the following errror:

SQLSTATE[07002]: [Microsoft][ODBC Driver 11 for SQL Server]COUNT field incorrect or syntax error (SQL: 'INSERT INTO bullets(product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) VALUES('?','?','?','?','?','?')', [1,1,can't,'N',2017-11-10 16:28:44,2017-11-10 16:28:44])

I have also tried:

 $bullets = Input::get('bullet_content');
        $product_id = Input::get('product_id');
        $user_id = Input::get('user_id');
        $retailer_id = Input::get('retailer_id');
        $date = date("Y-m-d H:i:s");
        foreach ($bullets as $bullet){


            $query = "'INSERT INTO bullets(product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) 
                        VALUES('?','?','?','?','?','?')' ";
            $values = [$product_id,$user_id,$bullet,'N',$date,$date];

                        DB::insert($query,$values);
        }
        return back()->with('message','Features add successfully!');

and got the following error:

SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'INSERT INTO bullets(product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) VALUES('. (SQL: 'INSERT INTO bullets(product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) VALUES('1','1','can't','N','2017-11-10 16:33:43','2017-11-10 16:33:43')' )
  • 写回答

1条回答 默认 最新

  • dongxian3418 2017-11-10 16:40
    关注

    You don't need to quote question marks. Furthermore you can initialize $query before loop and use it as your prepared query inside the foreach:

    $bullets = Input::get('bullet_content');
    $product_id = Input::get('product_id');
    $user_id = Input::get('user_id');
    $retailer_id = Input::get('retailer_id');
    $date = date("Y-m-d H:i:s");
    $query = "INSERT INTO bullets (product_id, user_id,bullet_content, bullet_deleted, created_at, updated_at) 
                           VALUES (?, ?, ?, ?, ?, ?)";
    foreach ($bullets as $bullet) {
        $values = [$product_id,$user_id,$bullet,'N',$date,$date];
        DB::insert($query, $values);
    }
    return back()->with('message','Features add successfully!');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错