f



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


0
soazine (387)
9/10/2003 11:10:21 PM
comp.lang.php 32646 articles. 0 followers. Post Follow

1 Replies
191959 Views

Similar Articles

[PageSpeed] 19

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
0
jon1117 (163)
9/11/2003 8:54:15 AM
Reply: