I am trying to UPDATE many columns in a mysql row using values passed from a webform.
I have to do this with several different tables and row combinations, so am trying to develop some reusable code, as coding in each QUERY with all the specified columns and parameters is a bit cumbersome and prone to alignment errors.
I think I am close to a solution that allows me to simply create a map of all the columns to be updated, making sure sure the associated $_REQUEST variables are named the same as the columns they correspond to.
However, I seem to be having issues with the formatting of the QUERY syntax having something to do with the parameter related to the WHERE section of the query.
Can anyone see what I am doing wrong in the below code?
//list of the columns to be updated. These are also the variable names passed from webform via $_REQUEST global
$map1 = array(
'name',
'status',
'rel',
't1',
't2',
't3',
't4',
'gtsize',
'rate',
'preauth',
'qbadden',
'nsrv',
'regreq',
'freq',
'gtaccess',
'id_tech',
'bonus',
'drumvac',
'fortyaccess',
'dispas',
'email_man',
'parent',
'id_reg',
'notes_dispatch',
'notes_tech',
'notes'
);
$placeholder = "";
$params = [];
foreach($map1 as $key) {
$placeholder .= ",$key=?";
$params[] = $_REQUEST[$key];
}
// Cutting off the first ',' here
$placeholder = substr($placeholder, 1);
//add the additional and last value for the WHERE ID=? parameter to the parameter list
$params[] = $_REQUEST['SRVID'];
// for debugging:
//ECHO '<br>'.$placeholder.'<br>';
//var_dump($params);
$query = 'UPDATE services SET $placeholder WHERE id=?';
$result = $conn->execute_query($query, $params);