www.webdeveloper.com
Results 1 to 5 of 5

Thread: problem in upload csv file to mysql database in wamp server

  1. #1
    Join Date
    Jun 2011
    Posts
    20

    Post problem in upload csv file to mysql database in wamp server

    I have csv file having data of Asian Countries.name of countries, years, progress in paultry .these are fields.
    when I upload the file, it does not upload. it gives errors.
    please observe this code and give sugestion .
    How can I improve this code.
    what should be structure of database so that all values in csv file should be uploaded in database.
    please observe this code.
    <?php
    include("opendb.php");
    //echo isset($_POST['submit']);
    if(isset($_POST['submit']))
    {
    $filename=$_FILES['files']['name'];
    $filename=date('U').$filename;
    $add1 = "import/$filename";
    move_uploaded_file($_FILES[files][tmp_name], $add1);
    chmod($add1,0777);
    basename($_FILES['files']['name'] );
    $filename1="import/$filename";
    $fcontents = file ($filename1);
    echo sizeof($fcontents);
    for($i=0; $i<sizeof($fcontents); $i++) {
    $line = addslashes(trim($fcontents[$i]));
    $arr = explode("\t", $line);
    echo "<pre>";
    print_r($arr);
    //exit();
    //echo $arr;
    $Repl_arr=array("[","]");
    //echo $Repl_arr;
    foreach($arr as $key=>$val)
    {
    $arr[$key]=str_replace($Repl_arr,"",$arr[$key]);
    $arr_new=$arr[$key];
    $arr_new1 = explode(",", $arr_new);
    }
    //echo "<PRE>";
    //var_dump($arr_new1);
    /*
    $date=explode("/", $arr_new1[0]);
    $dd=$date[0];
    $mm=$date[1];
    $yy=$date[2];
    if(strlen($dd)==1){
    $dd="0".$dd;
    }
    if(strlen($mm)==1){
    $mm="0".$mm;
    }
    $date1=$yy."-".$mm."-".$dd;
    */
    $name=$arr_new1[0];
    $addr = $arr_new1[1];
    //$email=$arr_new1[2];
    //$amount=$arr_new1[3];
    $sql = "insert into mytestupload set
    tfield='$name',tfield1='$addr'";
    //echo $sql;
    //exit();
    $qr=mysql_query($sql);
    echo "-----------1 Row Inserted----------";
    $sql ."<br><br>\n";
    if($qr!=0)
    $msg= "Data has been imported";
    if(mysql_error()) {
    echo mysql_error() ."<br>\n";
    }
    }
    unlink($filename1);
    unset($_POST);
    $_POST['submit']='';
    $_POST='';
    //echo "<script>window.location=('addcsv.php');</script>";
    } ?>
    <html>
    <head>
    <title>Import CSV</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="../css/decorate.css" rel="stylesheet" type="text/css">
    </head>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td align="center" valign="top"><table width="100%" border="0">
    <tr>
    <td height="25" align="center" class="label">Import CSV Tax File </td>
    </tr>
    </table>
    <table width="100%" border="0" cellpadding="3" cellspacing="0">
    <tr>
    <td valign="top"><form action="importdata.php" method="post" enctype="multipart/form-data">
    <table width="100%" border="1">
    <tr>
    <td width="30%" class="line2">File Name:</td>
    <td width="70%" align="left" class="line2"><input name="files" type="file" class="inp"></td>
    </tr>
    <tr>
    <td class="line2">&nbsp;</td>
    <td align="left" valign="middle" class="line2">&nbsp;</td>
    </tr>
    <tr><td class="line2">&nbsp;</td>
    <td align="left" valign="middle" class="line2">
    <input name="submit" type="submit" class="buttons" value="Submit" ></td>
    </tr>
    </table>
    </form></td>
    </tr>
    </table></td>
    </tr>
    </table>
    </body>
    </html>

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    What is the error message you see ?
    What is the expected output and what is the present output ?

  3. #3
    Join Date
    Jun 2011
    Posts
    20
    There is syntax error at many lines when i run this code in localhost.
    i want to upload my csv file to database.that is file data
    inflows(US$Million) 2001 2002 2003 2004 2005 2006 2007
    developcountry 94,896 111,018 137,434 159,295 192,127 226,707 278,456
    lincome 4,539 6,074 6,988 8,108 10,071 12,992 16,574 21,962
    mincome 32,735 36,160 47,457 57,690 67,791 77,854 87,074 92,158
    eap 20,998 27,018 32,279 40,013 50,300 57,440 71,073 85,465
    europecena 10,302 10,715 11,597 15,998 23,262 28,397 39,332
    lacarb 24,446 28,160 36,768 43,351 50,144 59,223 63,281 64,647
    mena 15,323 15,936 20,461 23,217 25,078 26,458 32,145 35,937
    South Asia 19,172 24,137 30,366 28,694 33,924 42,523 54,041
    Sub-Saharan Africa 6,599 8,367 9,538 10,767 11,944 14,160
    Fragile States 2,470 2,683 3,213 4,052 4,389 4,778 5,348
    Small States 4,179 4,594 4,821 5,419 5,352 6,579 8,382

    thanks.

  4. #4
    Join Date
    Apr 2010
    Posts
    227
    Start fault finding my friend.

    Firstly upload the csv file to the server and have your php script echo out the file details like size and name.

    Pass fail?

    If it passes check now parse the csv file line by line and echo out the output.

    Is the structure correct?

    Pass/fail?

    Now you can update your sql database line by line instead of echoing line by line.

    Basically break down your script because what you are trying to do is php bread and butter.

    ps: please use php code display brackets.

  5. #5
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Text has to be quoted, and numbers stripped of comma's (000's separators), or add slashes.

    Also your data does not seem to be in the right format.
    It appears you need to transpose the data from rows to columns.

    i.e.
    "year", "inflows", "developcountry", "lincome", "mincome", "eap", "europecena", "lacarb", "mena", "South Asia", "Sub-Saharan Africa", "Fragile States", "Small States"
    2001, 94896, 4539, 32735, etc.....
    2002, ...

    you can also insert many rows in a single command.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles