dongranding3909 2019-02-27 07:53
浏览 66
已采纳

我如何在我的其他表中使用id的值,因为我自动创建它

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!

  • 写回答

1条回答 默认 最新

  • doudong4532 2019-03-13 12:28
    关注

    I did it like this instead.

      public function createGodTable()
    {
        return $this->db->toList("CREATE TABLE IF NOT EXISTS `Angle`(
            id INT(11) AUTO_INCREMENT PRIMARY KEY,
            Name VARCHAR(50),
            Datum VARCHAR(50), 
            Property VARCHAR(50), 
            Criterion VARCHAR(50), 
            Type VARCHAR(50), 
            Nominal DECIMAL(10,2), 
            Actual DECIMAL(10,2), 
            Tolminus DECIMAL(10,2), 
            Tolplus DECIMAL(10,2), 
            Dev DECIMAL(10,2), 
            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 insertGodTable($myArray)
    {
        return $this->db->toList("INSERT INTO `Angle` (`Name`,`Datum`, `Property`, `Criterion`, `Type`, `Nominal`,
         `Actual`, `Tolminus`,`Tolplus`,`Dev`,`Palle_nr`,`Varenummer`,`Ordre_nummer`,`Operatoer`,`Maskine_nr`,`Raavare_batch_nr`,`Dato_ur_stillet`,`Klokke_fuldsvejst`,`fuldstoebning_af_klokke`,`Afgratning_af_overflade`,`Vaegt`)
        VALUES (:Name, :Datum, :Property, :Criterion, :Type, :Nominal, :Actual, :Tolminus,
         :Tolplus, :Dev, :Palle_nr, :Varenummer, :Ordre_nummer, :Operatoer, :Maskine_nr ,:Raavare_batch_nr, :Dato_ur_stillet, :Klokke_fuldsvejst, :Fuldstoebning_af_klokke, :Afgratning_af_overflade, :Vaegt)",
            [   
                ':Name' => $myArray[0],
                ':Datum' => $myArray[1],
                ':Property'=> $myArray[2],
                ':Criterion'=> $myArray[3],
                ':Type'=> $myArray[4],
                ':Nominal'=> $myArray[5],
                ':Actual'=> $myArray[6],
                ':Tolminus'=> $myArray[7],
                ':Tolplus'=> $myArray[8],
                ':Dev'=> $myArray[9],
                ':Palle_nr'=> $myArray[10],
                ':Varenummer'=> $myArray[11],
                ':Ordre_nummer' => $myArray[12],
                ':Operatoer'=> $myArray[13],
                ':Maskine_nr'=> $myArray[14],
                ':Raavare_batch_nr'=> $myArray[15],
                ':Dato_ur_stillet'=> $myArray[16],
                ':Klokke_fuldsvejst'=> $myArray[17],
                ':Fuldstoebning_af_klokke'=> $myArray[18],
                ':Afgratning_af_overflade'=> $myArray[19],
                ':Vaegt'=> $myArray[20]
            ]
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器