I'm having a bit of an issue. Basically what I am trying to do is the following;
- I am using PDO
- I want the ability to pass an array containing the column name in the db (the key) and the info I want to insert (value). The array may contain 1, or many fields that need to be updated.
- I have designed a function that looks like this:
Sample Array being passed:
$columnsToRetrieve = array('column1' => 'info',
'column2' => 'data',
'column3' => 'data');
And a sample function (did not include the DB initialization part)
function updateInfo ($columnsToRetrieve, $whereClauseValue) {
$counter = 1;
$queryString = 'UPDATE table SET ';
foreach ($columnsToRetrieve as $k => $V) {
$queryString .= $k . ' = ?';
}
$queryString .= 'WHERE column4 = ?'
$stmt = $dbc->dbConnection->prepare($queryString);
foreach ($columnsToRetrieve as $k => $v) {
$stmt->bindParam($counter, $v);
$counter++;
}
$stmt->bindParam($counter, $whereClauseValue);
$stmt->execute();
}
The problem is with the second foreach, where I am trying to use the bindParam($counter, $value). Although the right number and value populates, it doesn't seem to want to accept it.
Does anyone have any ideas how this can be done, or what I'm doing wrong above?
->execute( array(....) );dont you? And therefore you can loose thebindParam(),after each?and a space beforeWHERE. But who outputs variables now, pfffff.echo $queryString;should show you a few obvious errors