<?php 

include_once("class.dbf.php");
include_once("../../config.php");
include_once("helper.php");
global $sugar_config;
$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);

mysql_query("set character set utf8;");

$path = dirname(__FILE__).'/E-m-s/_MAG.DBF';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

$mysqli = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], $sugar_config['dbconfig']['db_name']);
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

$mag_allowed = array(
  '0',1,2,7,12,15,29,31,37,23,22      
);

$stmt=$mysqli->prepare("INSERT INTO ecmstocks (id,name,date_entered,date_modified,main,dir,no,sys_number) VALUES (?,?,now(),now(),?,?,?,?)");
$stmt->bind_param("ssssss", $id,$nazwa,$czy_glowny,$dir,$no,$sys_num);
echo "<pre>";
for($i=$num_recp;$i>=0;$i--){
    $rowp=$dbfp->getRowAssoc($i);
    echo $rowp["NR"]."\n";
    var_dump(array_search($rowp["NR"],$mag_allowed));
    if(array_search($rowp["NR"],$mag_allowed)!==false){
       
  
        $id=create_guid();
        $nazwa=ch($rowp["NAZWA"]);
        if($rowp["NR"]==0){
            $czy_glowny=1;
        } else {
            $czy_glowny=0;
        }
        $dir=ch($rowp["NR"]);
        $no=ch($rowp["NR"]);
        $sys_num=ch($rowp["NR_SYSTEM"]);
        echo $rowp["NR"]."dodanne\n";
        if (!$stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }
}
?>