Reading a .csv file into a MySql DataBase - 2 problems

  • Mark709
  • Novice
  • Novice
  • Mark709
  • Posts: 22

Post 3+ Months Ago

I have written this code (plus the config.php file - which works in another application)
but am obviously doing something wrong in the following code
(which I submit without the usual PHP open and close symbols of < etc and >)

PHP Code: [ Select ]
<?php
include "config.php";
if(isset($_POST['submit']))
   {
$filename=$_POST['filename'];
 
$datetransferred=$_POST['datetransferred'];
$known_as=$_POST['known_as'];
$email=$_POST['email'];
$dob=$_POST['dob'];
 
$filename="/markreada.csv";  // this comes from another file but unsure it works
 
//     $handle = fopen("$filename", "r");  // problems so fix a name and try that
     $handle = fopen("markreada.csv", "r");   //maybe this might work
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
$import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
VALUES('$datetransferred' , '$known_as' , '$email' , '$dob')";
       mysql_query($import) or die(mysql_error());
     }
     fclose($handle);
     print "Import done1";
   }
   else
   {
    print "Import done2";
   }
echo "I am here processing I hope";
?>
  1. <?php
  2. include "config.php";
  3. if(isset($_POST['submit']))
  4.    {
  5. $filename=$_POST['filename'];
  6.  
  7. $datetransferred=$_POST['datetransferred'];
  8. $known_as=$_POST['known_as'];
  9. $email=$_POST['email'];
  10. $dob=$_POST['dob'];
  11.  
  12. $filename="/markreada.csv";  // this comes from another file but unsure it works
  13.  
  14. //     $handle = fopen("$filename", "r");  // problems so fix a name and try that
  15.      $handle = fopen("markreada.csv", "r");   //maybe this might work
  16.      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  17.      {
  18. $import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
  19. VALUES('$datetransferred' , '$known_as' , '$email' , '$dob')";
  20.        mysql_query($import) or die(mysql_error());
  21.      }
  22.      fclose($handle);
  23.      print "Import done1";
  24.    }
  25.    else
  26.    {
  27.     print "Import done2";
  28.    }
  29. echo "I am here processing I hope";
  30. ?>


PROBLEMS
1. The above code DOES OPEN the DataBase yet BLANK RECORDS ARE SAVED
2. Ideally I would like to be able to type in various file names and their locations
on my home computer (rather than fix a name) eg C:\temp\readthisfile.csv
3. should the slash be (a) \ (as xp at home) or (b) / as host is Linux server
(if some kind person might be able to solve - much appreciated)
Many thanks
Moderator Remark: You can use [php]...[/php] to wrap code blocks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Code: [ Select ]
$import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
VALUES('$datetransferred' , '$known_as' , '$email' , '$dob')";
  1. $import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
  2. VALUES('$datetransferred' , '$known_as' , '$email' , '$dob')";


You might like to do this instead of upper one

Code: [ Select ]
$import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
VALUES('".$datetransferred."' , '".$known_as."' , '".$email."' , '".$dob."')";
  1. $import="INSERT INTO `raw_hotmail` (`datetransferred` , `known_as` , `email` , `dob`)
  2. VALUES('".$datetransferred."' , '".$known_as."' , '".$email."' , '".$dob."')";


'Single Quotes' means no rendering in PHP :)

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 159 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.