loop problem while the content of csv file is trying to insert into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumaabey
    New Member
    • Jun 2007
    • 29

    loop problem while the content of csv file is trying to insert into database

    my code
    [code=php]<?
    //include("../mysqlconnect.ph p");
    $link = mysql_connect(" localhost","roo t","")
    or die("Could not connect: " . mysql_error());
    mysql_select_db ("suma");

    include("includ e/upload.inc.php" );
    if(isset($_POST['upload']))
    {
    $deli=$_POST['delimeter'];

    $userfile=$_FIL ES['userfile']['name'];
    $userfile_tmp=$ _FILES['userfile']['tmp_name'];
    $userfile_type= $_FILES['userfile']['type'];
    $userfile_size= $_FILES['userfile']['size'];
    $dir="upload";
    $filename=basen ame($userfile);
    move_uploaded_f ile("$userfile_ tmp","$dir/$filename");
    $row= 1;
    $fp = fopen("$dir/$userfile", "r");
    $s="";
    $s1="";
    $s2="";

    while (($data = fgetcsv($fp, 100000, "$deli")) !== FALSE)
    {
    $num = count($data);
    $row++;
    if($row==2)
    {
    for ($c=0; $c < $num; $c++)
    {
    $s.=$data[$c]." VARCHAR (200) not null,";
    $s2.=$data[$c].",";
    $s1.="'$data[$c]',";
    }
    echo $sql="create table temp1(id int(20) not null auto_increment, ".$s."extra varchar(200) not null, primary key(id))";
    mysql_query($sq l)or die("could not create");

    $sql1="insert into temp1 (".$s2."extr a) values (".$s1."'choose ')";
    mysql_query($sq l1)or die("could not insert");
    }

    if($row > 2)
    {
    $s1="";
    for ($c=0; $c < $num; $c++)
    {
    $s1.="'$data[$c]',";
    }
    $sql2="insert into temp1 (".$s2."extr a) values (".$s1."'choose ')";
    mysql_query($sq l2)or die("could not insert");
    }
    }

    fclose($fp);
    }

    ?>

    <table>

    <form action="" method="post" enctype="multip art/form-data" name="form3" id="manu">

    <tr class="greyBG">
    <td width="60%" height="33"> <div align="left">Pl ease Enter the delimeter
    </div></td>
    <td width="40%"><in put type="text" name="delimeter "> </td>
    </tr>
    <tr><td>Uploa d CSV file</td><td><input name="userfile" type="file"></td>




    </tr>
    <tr><td colspan="2"> <input type="submit" name="upload" value="upload"> </td></tr>
    </form>
    </table>[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]
    Last edited by pbmods; Jul 9 '07, 01:20 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, sumaabey.

    What do you want your code to do? Give an example.
    What is your code doing that you don't want it to do? Give an example.
    What is your code *not* doing that it is supposed to? Give an example.

    Comment

    • sumaabey
      New Member
      • Jun 2007
      • 29

      #3
      i want to import csv file into mysql data can you help me?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, sumaabey.

        Originally posted by sumaabey
        i want to import csv file into mysql data can you help me?
        MySQL has a LOAD DATA INFILE command for that.

        Comment

        Working...