Jump to content

[SOLVED] Inserting csv file into specific columns


emexinc

Recommended Posts

...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>";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.