NerdConcepts Posted May 30, 2007 Share Posted May 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53515-solved-stupid-csv-query-issues/ Share on other sites More sharing options...
gabeg Posted May 30, 2007 Share Posted May 30, 2007 Your problem is the actual query. I believe column names can't have spaces in them $query = "INSERT INTO work_order (column 1, column 2, column 3) VALUES ('$data[0]','$data[1]','$data[2]')"; Quote Link to comment https://forums.phpfreaks.com/topic/53515-solved-stupid-csv-query-issues/#findComment-264472 Share on other sites More sharing options...
NerdConcepts Posted May 30, 2007 Author Share Posted May 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53515-solved-stupid-csv-query-issues/#findComment-264484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.