lundi 21 août 2017

How to add array data to mySql database

i have database structure like this.

Database Structure

i want to import this txt file to sql Txt File

this is my code to read the file. im already get the column name and value name as array. my question is, how to input it with sql query.

 <?php
        include 'connect.php';


        $line = file_get_contents("show_command_file_Tes/show_alarm_active.txt", "r");
        $fLine = explode("\n", $line);
        $size = count($fLine);
        $data = array();
        $arr_data;
        $temp = array();
        $column = array();//Kolom tabel database
        $value = array();//Nilai Kolom

        for ($i=0; $i < $size; $i++) {
            if (strpos($fLine[$i], " :") !==false) {
                $temp = $fLine[$i];
                ArrayData($temp);
            }
        }

        function ArrayData($temp)
        {
            global $fLine, $size,$data;
            for ($i=0; $i < count($temp) ; $i++) {
                $data = explode(" :", $temp);

                for ($j=0; $j < count($data); $j++) {
                    $arr_data[$i][$j] = $data[$j];
                    $kolom= trim($arr_data[$i][0], " ");    //remove space at the end of the strings
                $column = str_replace(' ', '_', $kolom); //change space with underscore


                if ($j==1) {
                    $value = $arr_data[$i][$j];
                }
                }
                InsertData($column, $value);
            }
        }

    function InsertData($column, $value)
    {
        for ($i=0; $i < count($column); $i++) {
            echo "KOLOM :".$column." --> VALUES :".$value."<br>";
            $query = "INSERT INTO show_alarm_active ('$column') VALUES ('$value')";

            $sql = mysqli_query($GLOBALS['conn'], $query);
        }
    }

    ?>




Aucun commentaire:

Enregistrer un commentaire