mactron Posted February 23, 2020 Share Posted February 23, 2020 I'm trying to insert array of posts categories into MySQL. I'm getting category IDs from drop down list.. ERR: Notice: Notice: Array to string conversion in C:\laragon\*********\includes\functions.php on line 477 Array Array ( [0] => Array ( [0] => 4 [1] => 5 ) ) PHP try { $sql = "INSERT INTO posts_category_ids (post_id, category_id) VALUES"; $insertQuery = array(); $insertData = array(); foreach ($filter_category as $row) { $insertQuery[] = '(?, ?)'; $insertData[] = $id; $insertData[] = $row; } if (!empty($insertQuery)) { $stmt = $this->conn->prepare($sql); $stmt->execute($insertData); //477 } }catch (Exception $e){ echo $e->getMessage(); } } Quote Link to comment https://forums.phpfreaks.com/topic/310116-insert-array-in-mysql/ Share on other sites More sharing options...
Barand Posted February 23, 2020 Share Posted February 23, 2020 I'm surprised it got past the prepare statement before giving an error. Where do $id and $row come from? Are the two sections of code above related in any way? If, say, you want to add 3 records inserting post_id and category_id into each then the query that gets prepared should look like "INSERT INTO posts_category_ids (post_id, category_id) VALUES (?,?),(?,?),(?,?)" Quote Link to comment https://forums.phpfreaks.com/topic/310116-insert-array-in-mysql/#findComment-1574833 Share on other sites More sharing options...
mactron Posted February 23, 2020 Author Share Posted February 23, 2020 From insert query for posts.. $id = $this->conn->lastInsertId(); I would like something like this.. +----+---------+-------------+ | id | post_id | category_id | +----+---------+-------------+ | 1 | 2 | 3 | | 2 | 2 | 5 | | 3 | 3 | 1 | | 4 | 3 | 2 | +----+---------+-------------+ The hole problem is inside my array. Quote Link to comment https://forums.phpfreaks.com/topic/310116-insert-array-in-mysql/#findComment-1574835 Share on other sites More sharing options...
mactron Posted February 23, 2020 Author Share Posted February 23, 2020 I just resolve the issue! Quote Link to comment https://forums.phpfreaks.com/topic/310116-insert-array-in-mysql/#findComment-1574840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.