problem in insert data ( 6 Views )

no kitty!
  1. i have found this coding that can display the data from csv but the problem is it didnt insert the data into mysql?why is this happen?

    <?php


    ## Connect to a local database server (or die) ##
    $dbH = mysql_connect('localhost', '', '') or die('Could not connect to MySQL server.<br>' . mysql_error());

    ## Select the database to insert to ##
    mysql_select_db('apple_sql') or die('Could not select database.<br>' . mysql_error());



    $file_name= $_FILES['csvfile']['name'];

    $columnheadings = 0; ##columnheadings,whether there is field name in csv file##
    $pass = 0;
    $fail = 0;

    $row = 1;
    $handle = fopen ("$file_name","r");
    while ($data = fgetcsv ($handle, 1000, ",")) {
    $num = count ($data);
    echo "<p> $num fields in line $row: <br>\n";
    $row++;
    for ($c=$columnheadings; $c < $num; $c++) {
    echo $data[$c];
    $insertrecord = "Insert Into `details` Values ($data[$c])";
    mysql_query($insertrecord);
    if(mysql_error()) {
    $fail += 1; # increments if there was an error importing the record
    }
    else
    {
    $pass += 1; # increments if the record was successfully imported
    }
    }
    }

    echo "fail" .$fail."</br>";
    echo "pass" . $pass;

    fclose ($handle);
    ?>

    (halil, Nicaragua)

  2. Maybe you shouldn't use the ' s around your tablename (details)...

    (face, Burkina Faso)

  3. PHP Code:

    $insertrecord = "Insert Into `details` Values ($data[$c])";

    I don't think you can access an array value within quotes like that. Try:
    PHP Code:

    $insertrecord = "Insert Into `details` Values (".$data[$c].")";


    (esra, Central African Republic)

  4. and better yet,
    PHP Code:

     $insertrecord = "INSERT INTO details VALUES ('{$data[$c]}')";

      
    /* or, my preferred way */
      
    $insertrecord = sprintf("INSERT INTO details VALUES ('%s')", $data[$c]);

    Basically, $data[$c] is a string and needs quotes around its value.

    (ali, Morocco)



Related Topics ... (or search in 1.720.883 topics !)

mysql insert data problem. any help? thanks. (6)
online survey tool - insert data problem (23)
is one big mysql insert quicker or just the same speed as a load data infile insert? (2)
help! i can't do one insert with multiple values to insert bulk data. (6)
file parsing and insert data into data base (4)
how to insert data into a db (3)
couldn't insert data (25)
insert data into $db (7)
insert data from .... (8)




copyright © 2007-2031 Pfodere.COM ( 3 Pfoyihuee Online )

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
1.6532