I can't insert into mySQL db table - HELP!

This is a discussion on I can't insert into mySQL db table - HELP! within the PHP Language forums, part of the PHP Programming Forums category; I have to get this done by tonight! I have a .CSV file and a PHP file where I am ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-11-2003
Phil Powell
 
Posts: n/a
Default I can't insert into mySQL db table - HELP!

I have to get this done by tonight! I have a .CSV file and a PHP file where
I am inserting the values of the CSV file into the db table.

See the results live: http://www.nordicnet.no/admin/alle_produkter.php and
watch it bomb nastily.

Here is the PHP code:

<?


require_once('/home/nordicnet.no/include/nordicnet_global_vars_functions.php
');

$fileID = fopen("$ACTUAL_STARTPATH/kalkyle-underwear-fruit.csv", 'r')
or die('Could not open CSV');

$dbConn = mysql_connect($dbServer, $dbUsername, $dbPassword) or die('Could
not connect');
if (!mysql_select_db($dbName, $dbConn)) die('Could not find db');

$rowNumber = 1; $sql = ''; $startSQL = '';
$varcharArray = array('varenr', 'navn', 'farge_code');

while ($row = fgetcsv($fileID, 100000, ',')) {
if ($rowNumber == 1) {
$startSQL .= 'INSERT INTO nnet_produkt(';
for ($i = 0; $i < sizeof($row); $i++)
$startSQL .= 'nnet_produkt_' . $row[$i] . ', ';
$startSQL = trim($startSQL, ', ') . ') VALUES (';
}
if ($rowNumber > 1) {
for ($i = 0; $i < sizeof($row); $i++) {
print_r("row number $rowNumber: " . $row[$i] . "\n<P>");
if (!in_array($row[$i], $varcharArray) && strlen($row[$i]) > 0) {
$sql .= $row[$i] . ', ';
} elseif (strlen($row[$i]) > 0) {
$sql .= "'" . mysql_escape_string(htmlspecialchars($row[$i])) . "', ";
} elseif (!in_array($row[$i], $varcharArray)) {
$sql .= 0 . ', ';
} else {
$sql .= "'', ";
}
}
$sql = $startSQL . trim($sql) . ')';
}
if ($rowNumber > 1) {
print_r($sql . "\n<P>");
if (!mysql_query($sql)) die('Could not perform insert: ' . $sql . ' ' .
mysql_error());
$sql = '';
}
$rowNumber++;
}

@mysql_free_result($query);
mysql_close($dbConn) or die('Could not close db');

?>

AND here is the CSV:

varegruppe_id,varenr,navn,farge_code,storrelse_id, kvalitet_id,innpris,utpris
,netto,forhandler_rabatt,quantity

1,17-220-3,Men's Underwear Slip (3-Pack),30,,,39.00,149.00,120.16,84.11,30

1,17-220-3,Men's Underwear Slip (3-Pack),53,,,39.00,149.00,120.16,84.11,30

1,17-206-2,Men's Underwear Classic(fly front 2
pack),30,,,42.50,149.00,120.16,84.11,30

1,17-206-2,Men's Underwear Classic(fly front 2
pack),54,,,42.50,149.00,120.16,84.11,30

1,17-208-1,Men's Underwear Sport(1-Pack),30,,,26.00,99.00,79.84,55.89,30

1,17-208-1,Men's Underwear Sport(1-Pack),36,,,26.00,99.00,79.84,55.89,30

1,17-208-1,Men's Underwear Sport(1-Pack),HC,,,26.00,99.00,79.84,55.89,30

1,17-208-2,Men's Underwear Sport(2-Pack),TH,,,39.00,149.00,120.16,84.11,30

1,17-208-2,Men's Underwear Sport(2-Pack),54,,,39.00,149.00,120.16,84.11,30

1,17-214-1,Men's Underwear Shorty(Sport Trunk 1
pack),30,,,30.00,129.00,104.03,72.82,30

1,17-214-1,Men's Underwear Shorty(Sport Trunk 1
pack),36,,,30.00,129.00,104.03,72.82,30

1,17-214-1,Men's Underwear Shorty(Sport Trunk 1
pack),HC,,,30.00,129.00,104.03,72.82,30

1,17-216-1,Men's Underwear Boxer(Reg Leg
1-Pack),30,,,30.00,129.00,104.03,72.82,30

1,17-216-1,Men's Underwear Boxer(Reg Leg
1-Pack),36,,,30.00,129.00,104.03,72.82,30

1,17-216-1,Men's Underwear Boxer(Reg Leg
1-Pack),HC,,,30.00,129.00,104.03,72.82,30

1,17-218-2,Men's Underwear Boxer Short(Woven
2-pack),CC,,,57.00,199.00,160.48,112.34,30

1,17-218-2,Men's Underwear Boxer Short(Woven
2-pack),CD,,,57.00,199.00,160.48,112.34,30

I'm stuck, please help!

Phil


Reply With Quote
  #2 (permalink)  
Old 09-11-2003
Jon Kraft
 
Posts: n/a
Default Re: I can't insert into mySQL db table - HELP!

Phil Powell <soazine@erols.com> wrote:

> I have to get this done by tonight! I have a .CSV file and a PHP file
> where I am inserting the values of the CSV file into the db table.
>
> See the results live: http://www.nordicnet.no/admin/alle_produkter.php and
> watch it bomb nastily.


Hi Phil,

What happens exactly? Error message(s)?
Get a login screen on that link - it was a very very bad idea to give it out
anyway.

JOn
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0