<?php
# ------------------------------------------- start addition -----------
// file: test.php
// require functions.php
require 'functions.php';
// just setting the test variables:
define('_USER_', 'natsuki'); // constant
$_host = 'localhost';
$_db = _USER_ . '_test';
$_user = _USER_ . '_test';
$_pass = 'test';
$database_freecrm = $_db;
// connect to db to get link
$freecrm = mysql_connect_db($_host, $_user, $_pass);
# ------------------------------------------- end addition --------------------
//connect to db or show error
mysql_select_db($database_freecrm, $freecrm)
or die(mysql_error());
//if form hidden field returns a value, execute the following script
if (isset($_POST['MM_insert']) && ($_POST['MM_insert'] == 'form1'))
{
//allocate posted file path to variable
$file = $_POST['filepath'];
// load file and split into array per newline "\n"
$temp = file_get_contents($file);
$temp = get_data_per_line($temp);
// or just use $arrfile from the start. I used temp because it's shorter xD
$arrfile = array(); // explicit declaration as array
$arrfile = $temp;
//$arrfile = explode(',',file_get_contents($file)); //
//loop through each line
foreach ($arrfile as $value)
{
// explicit array, to prevent $value from being an array itself
$data = array();
$data = explode(',', $value); // separate data per ','
//define column value
$firstname = str_replace('"', '', $data[0]); // just making safe
$lastname = str_replace('"', '', $data[1]);
$company = str_replace('"', '', $data[2]);
$firstname = format_db_string($firstname); // format string for db use
$lastname = format_db_string($lastname);
$company = format_db_string($company);
//insert into db
$insert = "INSERT INTO TEST (FIRSTNAME, LASTNAME, COMPANY)
VALUES ('$firstname', '$lastname', '$company');";
mysql_query($insert) OR die(mysql_error());
}
echo "Success putting data from csv $file to database $database_freecrm
and table TEST!";
}
?>