Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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") {
        //$well_no = $_POST['well_no'];
        $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 = []; // array of columns
$input = []; // array of prepared query input values

// add the well no
$input[] = $well_no; // my computed (not POST) variable
$col = ["`well_no`"];

        
        // loop to get array of field names and values for each
foreach($fields as $field)
{
	// note: empty considers 0 or '0' to be an empty value. this will prohibit a numerical zero value being used.
	//  instead test if the value is or is not an empty string.
	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
Posted
Updated 29-Jan-24 18:03pm
v3

1 solution

Your array_fill() is the culprit. Take a look at what you are putting after VALUES (
implode(',',array_fill(0,count($col),implode(',', $input)))

It's effectively evaluated right to left because of the nested function calls, so take the first (rightmost) implode(). That will string together the $input[] array, just like you did for $col[] earlier.
That's enough!
array_fill() then replicates that as many times as you have fields.

So, treat $input[] exactly as you did $col[]
 
Share this answer
 
Comments
Nicholas Lombardi 30-Jan-24 3:43am    
Yes, you hit the nail right on the head. I get a perfect INSERT statement. However, a nasty bug still persists Error: SQLSTATE[HY093]: Invalid parameter number: :well_no
I've been hearing that this error often crops up because PDO doesn't like duplicate column names appearing anywhere in the query. I'm trying to find if somehow my code is doing that behind the scenes. I'll have to research this error a bit further.
Peter_in_2780 30-Jan-24 5:37am    
Glad to help. As for SQL, what I know could comfortably be written on a postage stamp, so I'm bowing out right here.
Nicholas Lombardi 30-Jan-24 15:49pm    
thanks for your help, Peter!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900