1

I found this bit of code to import a local CSV file on my PC to a MYSQL table. I saved this file as import.php.

I found the code on this page

  <?php

//database connection details
$connect = mysql_connect('localhost','test','test');

if (!$connect) {
 die('Could not connect to MySQL: ' . mysql_error());
}

//your database name
$cid =mysql_select_db('shops',$connect);

// path where your CSV file is located
define('CSV_PATH','C:\Users\Administrator\Desktop\Images\CSV');

// Name of your CSV file
$csv_file = CSV_PATH . "Booth_shares.csv"; 


if (($handle = fopen($csv_file, "r")) !== FALSE) {
   fgetcsv($handle);   
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        for ($c=0; $c < $num; $c++) {
          $col[$c] = $data[$c];
        }

 $date = $col[0];
 $filename = $col[1];
 $directory = $col[2];
 $type = $col[3];
 $to = $col[4];

// SQL Query to insert data into DataBase
$query = "INSERT INTO tblshops(date,filename,directory,type,to) VALUES('".$date."','".$filename."','".$directory."','".$type."','".$to."')";
$s     = mysql_query($query, $connect );
 }
    fclose($handle);
}

echo "File data successfully imported to database!!";
mysql_close($connect);
?>

When I navigate to example.com/import.php, it says File data successfully imported to database!! But when I log on to MYSQL, there are no records.

I verify the CSV column names match the MYSQL field names.

Any ideas?

1
  • 1
    I found the source code to the game Doom on the Internet. That doesn't qualify me to ask "It doesn't work, why not?" questions. Do you have a specific question that involves some understanding of the code? Have you done ANY debugging yourself? Do you understand even a modicum of php, or have you looked at the PHP manual and tried to understand what it does. Commented Oct 10, 2016 at 2:21

1 Answer 1

1

Everything in my comment to you still holds true, however, it is highly possible that your problem is here:

define('CSV_PATH','C:\Users\Administrator\Desktop\Images\CSV');

Most probably what you actually need is:

define('CSV_PATH','C:\Users\Administrator\Desktop\Images\CSV\');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.