• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

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?
0
brucegust
Asked:
brucegust
  • 2
3 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now