SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Csv upload to mysql

    Hi i am a middle level php prgrammer i want to upload csv file to mysql and all rows have to store in mysql using php. Do u know any script sites and any ideas for.

    Thanks for all replys.

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using the following code to upload csv file to mysql using php but i am getting error the following error.

    Error: Query failed: Column count doesn't match value count at row 1

    But in csv file i used two columns name and email only, and i want to store these two columns only in mysql table

    <?php

    //we need function to get the csv
    function getcsv($filename, $delim =","){

    $row = 0;
    $dump = array(); //create new array for hold the data csv

    $f = fopen ($filename,"r");
    $size = filesize($filename)+1;
    while ($data = fgetcsv($f, $size, $delim)) {
    $dump[$row] = $data; //put the data to array
    //echo $data[1]."<br>";
    $row++;
    }
    fclose ($f);

    return $dump;
    }

    //this function for insert data to csv
    function makeINSERTS($text, $table){
    global $linkdata; //make global database connection
    $insert = array(); //make array for hold data insert
    $i = 0;

    while (list($key, $val) = @each($text)){

    $insert[$i] = "INSERT INTO `csv` ( `id` , `email` , `name` )
    VALUES ('', '";$insert[$i] .= implode("', '", $val);$insert[$i] .= "')\n";
    $result = mysql_query($insert[$i], $linkdata) or die('Query failed: ' . mysql_error());
    $i++;
    }

    return $insert;

    }

    //this verify is the file csv upload
    if ($_POST["submit"]=="submit") {
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

    $file=$_FILES['userfile']['tmp_name'];
    $linkdata = mysql_connect("localhost", "root", "")
    or die('Could not connect: ' . mysql_error());

    mysql_select_db("csv") or die('Could not select database');


    $CSVarray = getcsv($file);
    $CSVarray = makeINSERTS($CSVarray, "$tbl");


    } else {
    echo "error upload file";
    exit;
    }


    } else {
    //form upload
    echo "
    <FORM ENCTYPE=\"multipart/form-data\" ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=POST>
    Upload this file: <INPUT NAME=\"userfile\" TYPE=\"file\">
    <INPUT TYPE=\"submit\" VALUE=\"submit\" name=submit></FORM>
    ";
    }
    ?>

    Also let me know if any code like this to upload csv file to mysql using php.


    Thanks for reply

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by rengaraj View Post
    Error: Query failed: Column count doesn't match value count at row 1
    at the point you get this error, echo the exact query that was sent to mysql, and paste it here
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Result when i echo insert query

    INSERT INTO `csv` ( `id` , `email` , `name` ) VALUES ('', 'Query failed: Column count doesn't match value count at row 1

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    well, something is wrong with your php code, then, because it looks like you're trying to insert the error message itself!!!

    moving thread to php forum
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Member
    Join Date
    Oct 2006
    Location
    Glasgow
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it something to do with trying to insert 2 columns from your csv file into 3 columns in your mysql table.

    If you make sure that your
    Code:
    id
    field is set to
    Code:
    auto_increment
    in your Mysql table, then change the line

    PHP Code:
    $insert[$i] = "INSERT INTO `csv` ( `id` , `email` , `name` ) 
    to

    PHP Code:
    $insert[$i] = "INSERT INTO `csv` ( `email` , `name` ) 

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I also doubt in the number of columns you have in your CSV file. Check whether your CSV have only two fields 'Email' and 'Name' to fit your current code. Otherwise this seems pretty much working.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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