What does this error mean and do I need to worry about it?

I'm building an insert statement based on the presence of values in several pulldown menus in the preceding page.

It looks like this:

$insert_statement="";
			$handle=fopen("$csv_name", "r");
			$counter=0;
			while ((!$row=fgetcsv($handle))== false) 
			{
			$counter++;
				if($counter>1) //omit the column headings
				{
				//here's where you need to craft your query
				$insert_statement = 'insert into '.$table_name.'(';
				for($i=0; $i<=$_POST['column_count']; $i++)
				{
					if (isset($_POST['select_name_'.$i.'']) && $_POST['select_name_'.$i.'']=="")
					{
					header("location:csv_upload_error.php");
					exit();
					}
					else
					{
						$insert_statement.=$_POST['select_name_'.$i.''];
						if($i<$_POST['column_count'])
						{
							$insert_statement.=',';
						}
						else
						{
							$insert_statement.=', session_id)';
						}
					}
				}
				$insert_statement.=' VALUES (';
				for($i=0; $i<=$_POST['column_count']; $i++)
					{
						$the_row[$i]=$mysqli->real_escape_string($row[$i]);
						$insert_statement .='\'';
						$insert_statement .=$_POST['column_'.$i.''];
						$insert_statement .='\'';
						if($i<$_POST['column_count'])
						{
							$insert_statement.=',';
						}
						else
						{
							$insert_statement .=', \'';
							$insert_statement .="$the_session_id";
							$insert_statement .='\'';
							$insert_statement .=')';
						}
					}
				//echo $insert_statement;	
				//$query=$mysqli->query($insert_statement);
				}
			}
		}
		return $insert_statement;

Open in new window


The good news is that it works. I can print each individual insert statement and verify that it's sound. I can go to the database and see that the data has been correctly inserted.

But at the top of my page, I get this error: Warning: mysqli::query(): Empty query in C:\wamp\www\vbs\upload\csvClass.php on line 268

Line 268 is this guy right here: if(!$query=$mysqli->query($insert_statement))...

I can copy and paste the compiled insert statement into phpMyAdmin and it works fine. Data, everything - good to go. But I keep getting this error and I don't know why.

Any suggestions?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
It looks like your script try to run the query even in a moment $insert_statement is empty.
Try to do this:

if ( !empty( $insert_statement ) )
{
    if(!$query=$mysqli->query($insert_statement))...
}

Open in new window


This should ensure the query is run only if $insert_statement is not an empty string and so it should get rid of the warning.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Since that appears to be a function routine (because of the 'return' at the end), you probably need to declare '$mysqli' as a global for $the_row[$i]=$mysqli->real_escape_string($row[$i]); to work.  Unlike the older 'mysql' functions, $mysqli is a variable and is not automatically available in functions.
0
Ray PaseurCommented:
At the top, I see this:

$insert_statement="";

Then there are a bunch of conditional statements like while() or if() - none of these can be interpreted from the code alone, because they depend on the data to make decisions.

Then we see
Line 268 is this guy right here: if(!$query=$mysqli->query($insert_statement))...
so this leads me to believe that something about the data used in those conditional statements prevented the script from filling in the $insert_statement variable with anything that was meaningful to MySQL.  I would start by using var_dump() to see what is in the variables that get used in the conditional statements.
0
Marco GasiFreelancerCommented:
To clarify my thought,  I agree with Dave thinking that it's a function routine. I just think in your script that routine is called more than one time: probably it's called when values are the expected one to execute the query and so the query is executed and record is insertedd in the database and a second time i called when values are not the expected ones so $insert_statement remains empty and you get the warning message.
So perhaps you can investigate if you suppress one of the function calls but if you can't, checking if $insert_statement is empty before to execute it can solve the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.