how can i write my data from text file into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetmca
    New Member
    • Jan 2010
    • 15

    #1

    how can i write my data from text file into database

    hi..
    i m trying to write data from txt file onto database but when i am executing my php code for it the empty record will be inserted in the database not the data written on txt file will be entered.plz help me out
    [code=php]
    <html>
    <?

    $con=mysql_conn ect("localhost" ,"root","root") ;
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db ('test',$con);
    //(1) Read the text file into a variable
    $file = "testfile.t xt";
    $fp = fopen($file, "r");
    $data = fread($fp, filesize($file) );
    fclose($fp);


    //(2) Then we can get rid of the tabs in there:

    $output = str_replace("\t |\t", "|", $data);


    //(3) Then we explode it at every line break

    $output = explode("\n", $output);


    //(4) Then we loop through all the array elements and explode them again and insert them into mysql

    foreach($output as $var)
    $tmp = explode("|", $var);
    $FirstName = $tmp[0];
    $LastName = $tmp[1];
    $Age = $tmp[2];
    $sql = "INSERT INTO student SET FirstName='$Fir stName', LastName='$Last Name',Age='$Age '";
    mysql_query($sq l);



    echo "Done!";
    mysql_close($co n);
    ?> </html>
    [/code].
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    First of all, your foreach loop is - undoubtedly - not working as you want it to. Anything that should belong to a loop should be put inside brackets following the loop.

    If the foreach is not followed by brackets ({}), it will assume only the very first line following it belongs to the loop. Therefore you should ALWAYS use brackets. Even if there is only one line. (For the sake of consistency)
    [code=php]<?php
    // Some random values to loop through.
    $values = array("first", "second", "third");

    // This only prints one value: "third"
    foreach($values as $_value)
    $value = $_value; // This is inside the loop
    echo $value; // This is OUTSIDE the loop.

    // This prints all three values.
    foreach($values as $_value) {
    $value = $_value;
    echo $value;
    }
    ?>[/code]

    Also, you should verify that the data is valid before using it in the query. Make sure the values you are using aren't empty, and that they contain the data you expect them to contain.

    And as always, when dealing with MySQL queries: run ALL values through mysql_real_esca pe_string before putting it into the query. Otherwise your database is open for SQL Injection attacks. (If you think the data you are using is secure... you are wrong! :P)
    Last edited by Atli; Jan 28 '10, 11:13 AM. Reason: mysql_real_escape_string, not mysql_real_escape_query xD

    Comment

    • dgreenhouse
      Recognized Expert Contributor
      • May 2008
      • 250

      #3
      I see said the blind man... :-) I didn't notice the brackets... :-)

      Is that really how the data is delimited?
      i.e. with \t|\t

      Code:
      <?php
      // This works...
      $b = "\t|\t";
      $input = "John\t|\tSmith\nMary\t|\tJohnson";
      $output = str_replace($b,'|',$input);
      $output = explode("\n",$output);
      foreach($output as $var) {
        $tmp = explode("|",$var);
        echo $tmp[0] . ' ' . $tmp[1] . '<br>';
      }
      
      ?>
      Also, if the file isn't very large, you can use the 'file()' command to create an array of lines. Then you can explode each line (as you're doing) on your replaced delimiter ('|').
      Last edited by dgreenhouse; Jan 28 '10, 11:01 AM. Reason: Saw Atli's post

      Comment

      • puneetmca
        New Member
        • Jan 2010
        • 15

        #4
        thanx for d help..........

        Comment

        Working...