Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rotkivagrav's avatar

mysql query escape single quote

Hello, i have a problem with this following db::insert command. I want to use array in a mysql query "in ()" section, but the commas has been escaped -> in ('435','1671','429') How can i solve this problem?

<?php
$users = [435,1671,429];
$list = implode("','",$users);

DB::insert("insert into ".
    "rejections (calendar_id, user_id, note, status) ".
    "select id as calendar_id, ?, concat(?), concat(?) ".
    "from calendar ".
    "where DATE(_date) BETWEEN ? AND ? and user_id in (?)", .
    [$admin, $note, $status, $start_date, $end_date, $list]
);

mysql_log: 2019-05-30T16:28:51.221007Z 1815 Execute insert into rejections (calendar_id, user_id, note, status) select id as calendar_id, 2416, concat('Comment'), concat(1) from calendar where DATE(_date) BETWEEN '2019-05-01' AND '2019-05-31' and user_id in ('435','1671','429')

0 likes
2 replies
jlrdw's avatar

Please properly format your code.

https://laracasts.com/discuss/channels/general-discussion/guidelines-for-posting-on-laracastscom

I normally just get the data in an array and insert:

$postdata = array(
                'dogpic' => $dogpic,
                'dogname' => $dogname,
                'sex' => $sex,
                'comments' => $comments,
                'adopted' => $adopted,
                'lastedit' => $lastedit
            );

            DB::table('dc_dogs')->insert($postdata);

Remember Jeffrey has free intro videos to laravel, and a free php course.

Snapey's avatar

Pass the array rather than exploding it.

Also, make use of query builder rather than concatenating strings.

Please or to participate in this conversation.