Jump to content

Recommended Posts

Ok, I'm writing a script to take my .csv files and put them into MySQL, just doing this so other people can later upload there. Don't want other people having access to phpMyAdmin and all. The problem I am having is with running the query.

 

Here is my code:

 

<?PHP
require('includes/init.php');

if(isset($_POST['submitted'])) {
$handle = fopen($_FILES['csvUpload']['tmp_name'], "r");

$x = 0;

while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

	$query = "INSERT INTO work_order (column 1, column 2, column 3) VALUES ('$data[0]','$data[1]','$data[2]')";
	$result = mysql_query($query) or trigger_error("Query: $query\n<br />MySql Error: " . mysql_error());

	if($x >0) echo "$data[0] $data[1] $data[2]<br />";
	$x++;
}

} else {
?>
<form enctype="multipart/form-data" action="import.php" method="post">
		<fieldset>
			<legend>Test CSV File Upload</legend>
			Select CSV File to Upload
			<input type="file" name="csvUpload" />
			<br />
			<input type="hidden" name="submitted" value="TRUE" />
			<input type="submit" name="submit" value="Import" />
		</fieldset>
</form>
<?PHP
}
?>

 

the CSV file looks like this....

test,test,test

 

it grabs the loaded file and comes up with this as an error...

Query: INSERT INTO work_order (column 1, column 2, column 3) VALUES ('test','test','test')
MySql Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column 1, column 2, column 3) VALUES ('test','test','test')' at line 1 

 

but if I go ahead and right below the query ask it to display $data[0] etc it does. Shows up as "test" without the quotes. So I really have absolutely no idea what is wrong. The column names are completely correct. When I run the query itself in phpMyAdmin is get the same type of error. I'm running each column 1/2/3 as "text". I've tried VARCHAR[255]. Yet, still the same problems. I know I'm missing something, if someone could help out I could maybe sleep, lol. Thanks a bunch for previous help.

Link to comment
https://forums.phpfreaks.com/topic/53515-solved-stupid-csv-query-issues/
Share on other sites

Your right, wow, I'm dumb. Kind of sucks that you can't, now I am going to have to highly modify the actual csv database, there there is a main table and 69 fields; which most of those from the CSV file have spaces, I was just going to do it where I grabbed the file, read the first line which would have the "headers" which would match the table field and go ahead and put them in. Oh well, just means I right more code then I originally wanted to, but at least I only have to write it once. Thanks a bunch, I knew it was something stupid.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.