I have a form where I expect many blank input text boxes (standard html)
I need to insert records, with the INSERT statement ignoring those fields which do not have input values. For context, I take the POST variables in the usual way:
if ($_SERVER['REQUEST_METHOD']=="POST") {
$easting = $_POST['easting'];
$northing = $_POST['northing'];
etc etc for 15 variables. $well_no is not a POST variable, but computed from 2 other POST variables:
$well_no = $date_completed."-".substr($country,0,2)."-".mt_rand(1,9).mt_rand(10,50);
What I have tried:
I have tried the following approach:
<pre>$fields = [ 'easting', 'northing', 'purpose', 'country', 'admin1', 'admin2', 'admin3', 'settlement',
'orig_wellno', 'date_completed', 'coord_sys', 'elev', 'status'];
$col = [];
$input = [];
$input[] = $well_no;
$col = ["`well_no`"];
foreach($fields as $field)
{
if($_POST[$field] !== '')
{
$col[] = "`$field`";
$input[] = $_POST[$field];
}
}
try {
sql = "INSERT INTO well_parent (".implode(',',$col).") VALUES (".implode(',',array_fill(0,count($col),implode(',', $input))).")";
$stmnt = $pdo->prepare($sql);
$user_data = [':well_no'=>$well_no, ':easting'=>$easting, ':northing'=>$northing, ':purpose'=>$purpose, ':country'=>$country,
':admin1'=>$admin1, ':admin2'=> $admin2, ':admin3'=>$admin3, ':settlement'=>$settlement, ':orig_wellno'=>$orig_wellno,
':date_completed'=> $date_completed, ':coord_sys'=> $coord_sys, ':elev'=>$elev, ':status'=>$status];
echo '<pre>'; print_r($user_data); echo '
';
echo $sql;
$stmnt->execute($user_data);
Here is my output when I print _r the $user_data array:
Array
(
[:well_no] => 2017-05-09-Ug-922
[:easting] =>
[:northing] =>
[:purpose] => production
[:country] => Uganda
[:admin1] => mbale
[:admin2] => nomu
[:admin3] => booboo
[:settlement] => lop
[:orig_wellno] => bh1
[:date_completed] => 2017-05-09
[:coord_sys] =>
[:elev] => 200
[:status] => 1
)
So the $user-data array looks good. I also echo the sql statement:
INSERT INTO well_parent (`well_no`,`purpose`,`country`,`admin1`,`admin2`,`admin3`,`settlement`,`orig_wellno`,`date_completed`,`elev`,`status`) VALUES (2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1,2017-05-09-Ug-538,production,Uganda,mbale,nomu,booboo,lop,bh1,2017-05-09,200,1)Error: SQLSTATE[HY093]: Invalid parameter number: :well_no
I get the correct values in the values clause, but repeated 11 times! (Notice the warning I believe the SQL error mentions :well_no because it is the first problem parameter it encounters. I know the problem lies within the array_fill function, and using impode() as the values parameter, yet I'm lost on how to fix this