emexinc Posted December 29, 2008 Share Posted December 29, 2008 ...all i want to create is a simple update/insert...now i have an option to browse for the csv file that i want to have inserted into my db, but then i just get an "error, query failed"...as well, shouldn't i have to include the database and password information somewhere?...please point or kick me in the right direction...thanks <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $item_number_2 = $_FILES['userfile']['item_number_2']; $price = $_FILES['userfile']['price']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO biglist (item_number_2, price ) ". "VALUES ('$item_number_2', '$price')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/ Share on other sites More sharing options...
rhodesa Posted December 29, 2008 Share Posted December 29, 2008 first, change this: mysql_query($query) or die('Error, query failed'); to something more useful: mysql_query($query) or die('Error, query failed: '.mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725530 Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 please point or kick me in the right direction...thanks I prefer kick. Could you echo out $query and see what it actually looks like. You obviously have a mysql_error but your syntax looks correct. Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725535 Share on other sites More sharing options...
rhodesa Posted December 29, 2008 Share Posted December 29, 2008 i looked it over again, and some things don't make sense. #1 You never establish a database connection #2 Where are you getting 'item_number_2' and 'price' from? Those array keys won't exist $item_number_2 = $_FILES['userfile']['item_number_2']; $price = $_FILES['userfile']['price']; #3 $tmpName is never defined #4 You read the contents in and don't do anything with it #5 $fileName is never defined Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725540 Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 emexinc, You may want to post ALL of the relevant code (I assume there's more) so we can help you and not guess. Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725554 Share on other sites More sharing options...
emexinc Posted December 29, 2008 Author Share Posted December 29, 2008 http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx ...the url above is where i've based my code off of, i have shown you all the code that i have on my page, but the connection to the database is missing, so i'll need to add that, what i hope to do is insert information into specific columns even though the database has more columns...thanks <html> <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $item_number_2 = $_FILES['userfile']['item_number_2']; $price = $_FILES['userfile']['price']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } mysql_connect("host", "username", "pwd") or die(mysql_error()); mysql_select_db("dwditems2") or die(mysql_error()); $query = "INSERT INTO biglist (item_number_2, price ) ". "VALUES ('$item_number_2', '$price')"; mysql_query($query) or die('Error, query failed: '.mysql_error()); echo "<br>File $fileName uploaded<br>"; } ?> </html> ...i've inserted the connection to the database ( thank you ), and it replies with a "file uploaded", but when i check the database there is only a blank line that has been inserted... Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725604 Share on other sites More sharing options...
fenway Posted December 29, 2008 Share Posted December 29, 2008 emexinc, You may want to post ALL of the relevant code (I assume there's more) so we can help you and not guess. NO! Never post ALL of the code... we never need to see it all. Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-725725 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 emexinc, You may want to post ALL of the relevant code (I assume there's more) so we can help you and not guess. NO! Never post ALL of the code... we never need to see it all. Why wouldn't you want to see all the RELEVANT code? I agree with not all the code, even though a lot of people do that, although it is better than no code. Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-726163 Share on other sites More sharing options...
fenway Posted January 2, 2009 Share Posted January 2, 2009 Yes, all of the relevant code is ideal -- but people see "all", not "relevant". I prefer to ask for "relevant code snippet", it seems to result in fewer code dumps. As for it being better than nothing at all, if we respond to code dumps, there's little incentive for OPs to go find the relevant section of code. Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-727883 Share on other sites More sharing options...
Maq Posted January 2, 2009 Share Posted January 2, 2009 but then i just get an "error, query failed"... Is this all you get? Because this line: or die('Error, query failed: '.mysql_error()); Should give you more of a specific error... Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-728187 Share on other sites More sharing options...
emexinc Posted January 4, 2009 Author Share Posted January 4, 2009 ...that's all that i was getting in the error reporting... ...thanks for trying to help me with this problem, i have decided and found a way of doing this in two steps instead of one since it seems that i'll never get this to work... Quote Link to comment https://forums.phpfreaks.com/topic/138760-solved-inserting-csv-file-into-specific-columns/#findComment-729363 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.