P粉3431416332023-08-29 17:56:37
You don't provide the table structure it's inserting into, but if you just want to solve the problem of splitting the $data array into its component parts, there are a few ways:
a) Use implode(), which should work fine despite you already mentioned trying it:
$data = [1,2,3]; $sql = "INSERT INTO test_table VALUES (null,".implode(',',$data).")";
b) Quote each array index:
$data = [1,2,3]; $sql = "INSERT INTO test_table VALUES (null,{$data[0]},{$data[1]},{$data[2]})";
But this only works if there is a fixed number of values in the array.
c) Traverse the array:
$data = [1,2,3]; $sql = "INSERT INTO test_table VALUES (null" foreach($data as $value){ $sql .= ",$value"; } $sql .= ")";
Hope this helps, if not please provide more details about the data being inserted and the database table structure so we can better understand the problem.