I have these 2 tables palle_tb
and csvName
i am trying to use the id
from palle_tb
inside my csvName
using FK_Name
the tables are connected but i dont get any value inside the FK_Name.
I am pretty sure i should a variable inside ':FK_Name' =>
but dont know what to write since i already got this FK_Name INT(11),
FOREIGN KEY (FK_Name) REFERENCES palle_tb(id),
This is my code:
public function createPalleTable($latest_filename){
return $this->db->toList("CREATE TABLE IF NOT EXISTS `palle_tb`(
id INT(11) AUTO_INCREMENT PRIMARY KEY,
Palle_nr VARCHAR(50),
Varenummer VARCHAR(50),
Ordre_nummer VARCHAR(50),
Operatoer VARCHAR(50),
Maskine_nr VARCHAR(50),
Raavare_batch_nr VARCHAR(50),
Dato_ur_stillet VARCHAR(50),
Klokke_fuldsvejst VARCHAR(50),
Fuldstoebning_af_klokke VARCHAR(50),
Afgratning_af_overflade VARCHAR(50),
Vaegt DECIMAL(10,2))
");
}
public function insertPalleTable($latest_filename ,$palledata){
return $this->db->toList("INSERT INTO `palle_tb` (`Palle_nr`, `Varenummer`, `Ordre_nummer`, `Operatoer`, `Maskine_nr`,
`Raavare_batch_nr`, `Dato_ur_stillet`,`Klokke_fuldsvejst`,`Fuldstoebning_af_klokke`,`Afgratning_af_overflade`,`Vaegt`)
VALUES (:Palle_nr, :Varenummer, :Ordre_nummer, :Operatoer, :Maskine_nr, :Raavare_batch_nr, :Dato_ur_stillet,
:Klokke_fuldsvejst, :Fuldstoebning_af_klokke, :Afgratning_af_overflade, :Vaegt)",
[
':Palle_nr' => $palledata[0],
':Varenummer'=> $palledata[1],
':Ordre_nummer'=> $palledata[2],
':Operatoer'=> $palledata[3],
':Maskine_nr'=> $palledata[4],
':Raavare_batch_nr'=> $palledata[5],
':Dato_ur_stillet'=> $palledata[6],
':Klokke_fuldsvejst'=> $palledata[7],
':Fuldstoebning_af_klokke'=> $palledata[8],
':Afgratning_af_overflade'=> $palledata[9],
':Vaegt'=> $palledata[10]
]
);
}
/*The idea with the functions under this is that it creates a table with the Name of the data only and creates a foreign key to connect them*/
public function createCsvTable(){
return $this->db->toList("CREATE TABLE IF NOT EXISTS `CsvName`(
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FK_Name INT(11),
FOREIGN KEY (FK_Name) REFERENCES palle_tb(id),
Name VARCHAR(50))
");
}
public function insertCsvData($myArray){
$this->db->query("INSERT INTO CsvName (Name,FK_Name)
VALUES (:Name,:FK_Name)",
[
':Name' => $myArray[0], /*This array contains all my data, but when i add the [0] it only takes the Name*/
':FK_Name' => /*This should contain the id from the palle_tb i need help with how to get that*/
]
);
}
If you need the button then here is the code for that. I need it to be automatic so a select with the foreign key wont do.
if (isset($_POST['btn-upload'])){
copy("$sourcepath/$latest_filename","$copy/$latest_filename");
// Here i split the csv file, from second line. and using the first and fourth lines as the headers.
if (($openfile = fopen("$copy/$latest_filename", "r")) !== false) {
$header1 = fgetcsv($openfile, 1000, ";"); // consume, but don't use
$csv->createPalleTable($latest_filename);
$csv->insertPalleTable($latest_filename, array_map("toNumber", fgetcsv($openfile, 1000, ";")));
$delimiting_row = fgetcsv($openfile, 1000, ";"); // consume, but don't use
$header2 = fgetcsv($openfile, 1000, ";"); // consume, but don't use
while($row = fgets($openfile)){
// can parse further $row by usingstr_getcsv
$myArray = explode(';', $row);
$myArray = array_map("utf8_encode", $myArray);
$myArray = array_map("toNumber", $myArray);
echo'<pre>';
print_r($myArray);
echo'</pre>';
$csv->createCsvTable();
$csv->insertCsvData($myArray);
}
}
fclose($openfile);
}
Any help is appreciated!