0

I have a Json file 'extract.json', i get content for loop through result, but my code return only one row, My script ==>

$filename  = "extract.json";
$str = file_get_contents($filename);
if($str!=null) {
    $decoded=json_decode($str,true);
    $compteur = 0;
    $compteur =  count($decoded);
    if(is_array($decoded)) {
        foreach($decoded as $value) {
            $var1= $value["val1"];
            $var2 = $value["val2"];
            $var3 = $value["val3"];
            $var4 = $value["val4"];
            $var5 = $value["val5"];....

            $injection_final = "INSERT INTO `Table`(
                `var1`,
                `var2`,
                `var3`,
                `var4`,
                `var5`,)
                VALUES
                ($var1,$var2,$var3,$var4,$var5)";
            $inj = mysqli_query($con1,$injection_final);
        }
    }
}

MY JSON FILE ==>

[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}...]

4 Answers 4

1

The problem is in your sql, so it should not insert even a single row. you are using dates but haven't put them in quotes.Try like below:

 $filename  = "extract.json";
    $str = file_get_contents($filename);
    if($str!=null){
        /////.....rest of your code

                 $var1= $value["val1"];
                $var2 = $value["val2"];
                $var3 = $value["val3"];
                $var4 = $value["val4"];
                $var5 = $value["val5"];

                $injection_final = "INSERT INTO Table(var1,var2,var3,var4,var5,)
                VALUES($var1,$var2,'$var3',$var4,'$var5')";
                echo $injection_final."<br>";//use this query to insert

        ......
    }
Sign up to request clarification or add additional context in comments.

10 Comments

@Belicoff are you able to see the sqls echoed.
Yes of course i say it clearly brother :) but query excute only one every time :( :(
@Belicoff run the echoed sql directly in your phpmyadmin and see if inserted. did you check your connection string.what are you trying to return.
OOOps, i have an address with apostophe char that block the insertion, it's the error i think
@Belicoff in that case use mysqli_real_escape_string if you are using mysqli.
|
1
<?php

$json_data = '[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}]';
//uncomment the below line to read from file
//$json_data = file_get_contents("extract.json");
$array_data = json_decode($json_data, true);

for ($i = 0; $i < count($array_data); $i++) {
    $table_column_names = "";
    $table_column_values = "";
    foreach ($array_data[$i] as $coulmn_name => $field_value) {
        $table_column_names  .= "`$coulmn_name`, ";
        $table_column_values  .= "'$field_value', ";
    }
    $table_column_names = rtrim($table_column_names, ", ");
    $table_column_values = rtrim($table_column_values, ", ");
    $sql_query = "INSERT INTO `Table` ($table_column_names) VALUES ($table_column_values)";
    //excute query here -- like if i just print the query
    echo $sql_query."\n";
}

Here is what you need .. it will execute all rows Output :

INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')

1 Comment

I have been tried this, but the table insertion don't have a same column of my json file I want to parse some any field :)
1

Simple change in this line from $var1,$var2,$var3,$var4,$var5)"; to

('$var1', '$var2', '$var3', '$var4', '$var5')";

Comments

0

The format of your JSON array is not correct. Please refer to the following link for an example:

http://www.w3schools.com/js/js_json_arrays.asp

1 Comment

no man his json data is correct . you can check here:json.parser.online.fr (remove dots in the last).his sql is the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.