I am working on a project that requires me to do a database insert with quite some columns to fill. I tried making my function dynamic so that I didn't have to type 28 insert into- columns, 28 placeholders, and then bind 28 values to 28 placeholders....and in the near future I might want to add more columns, so here's how I tried to set it up, the whole function and arrays are a bit to much to post here so I'll stick to the essential part with some psuedo code for my question:
-In my form, there are up to 28 inputs, and the input names are the same as the database column name -The essential part of my code is as follows (some replaced by pseudo code to shorten this post)
<?php //stacks holds 28 values in total $stacks = array('name1','name2','name3','blablabla','etcetera'); // stacksDP is filled in a loop, //contains the same as $stacks but it has a : //in front of every value. $stacksDP = array() // :name1',':name2', .... $data = array(); // $data[stack / columnname] = "whatever"; $execArray = array(); //$execArray is filled like this in a foreach loop on $stacks as $stack: //$execArray[$stacksDP[$stack]] = $data[$stack]; // so far so good, but now I want to make the actual query and the following gives me a "Syntax error or access violation: 1064 You have an error in your SQL syntax; " $sql = "INSERT INTO stackoverflowplaceholder (".implode("','",$stacks)." VALUES ("; $sql .= implode(",",$stacksDP); // :columnname, $sql .=")"; $q = $this->conn->prepare($sql); echo $q->execute($execArray);
Any help on this matter would be appreciated :) Is it even possible what I'm trying to achieve here?