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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    #1

    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_globa l_vars_function s.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_st ring(htmlspecia lchars($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_res ult($query);
    mysql_close($db Conn) or die('Could not close db');

    ?>

    AND here is the CSV:

    varegruppe_id,v arenr,navn,farg e_code,storrels e_id,kvalitet_i d,innpris,utpri s
    ,netto,forhandl er_rabatt,quant ity

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

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

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

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

    1,17-208-1,Men's Underwear Sport(1-Pack),30,,,26.0 0,99.00,79.84,5 5.89,30

    1,17-208-1,Men's Underwear Sport(1-Pack),36,,,26.0 0,99.00,79.84,5 5.89,30

    1,17-208-1,Men's Underwear Sport(1-Pack),HC,,,26.0 0,99.00,79.84,5 5.89,30

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

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

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

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

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

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

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

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

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

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

    I'm stuck, please help!

    Phil


  • Jon Kraft

    #2
    Re: I can't insert into mySQL db table - HELP!

    Phil Powell <soazine@erols. com> wrote:
    [color=blue]
    > 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.[/color]

    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

    Comment

    Working...