2

I'm having trouble with inserting the values from this array. Here example:

$arr = json_decode($_POST['dynfields'], true);
    //{"dynfields":{"dynfields[0][DescRepair]":"Desc repair","dynfields[0][NestParts]":"Parts","dynfields[0][EdPrice]":"10","dynfields[0][DateRepair]":"2015-07-20","dynfields[1][DescRepair]":"Desc repair","dynfields[1][NestParts]":"Parts","dynfields[1][EdPrice]":"5","dynfields[1][DateRepair]":"2015-07-20"}}

    foreach ($arr as $key => $fieldArray ) {
        foreach($fieldArray as $k => $v) {
            echo $k . " - " . $v . "<br>"; // result: dynfields[0][DescRepair] - Desc repair
                                            dynfields[0] [NestParts] - Parts
                                            dynfields[0][EdPrice] - 10
                                            dynfields[0][DateRepair] - 2015-07-20
                                            dynfields[1][DescRepair] - Desc repair
                                            dynfields[1][NestParts] - Parts
                                            dynfields[1][EdPrice] - 5
                                            dynfields[1][DateRepair] - 2015-07-20

        }
        //$query = mysqli_query($mysqli, "INSERT INTO repair (DescRepair, NestParts, EdPrice, DateRepair) VALUES ('?', '?', '?', '?')") or die(mysqli_error($mysqli));  
    }

This is my code, but I don't know how to insert the value in db. Can you give me any suggestions. Thanks.

2 Answers 2

1

I did not understand very well your code, but at first your json is bad, on post, this is an example of right json:

{
"dynfields": [
    {
        "DescRepair": "Desc repair",
        "NestParts": "Parts",
        "EdPrice": "10",
        "DateRepair": "2015-07-20"
    },
    {
        "DescRepair": "Desc repair",
        "NestParts": "Parts",
        "EdPrice": "5",
        "DateRepair": "2015-07-20"
    }
  ]
}

Then you can make a foreach with the dynfields data:

$myvar = json_decode($json,true);
$data = $myvar['dynfields'];


    foreach(array_keys($data) as $index){
        var_dump($data[$index]);
    }

then you will get something like this (var_dump):

 array (size=4)
      'DescRepair' => string 'Desc repair' (length=11)
      'NestParts' => string 'Parts' (length=5)
      'EdPrice' => string '10' (length=2)
      'DateRepair' => string '2015-07-20' (length=10)
 array (size=4)
      'DescRepair' => string 'Desc repair' (length=11)
      'NestParts' => string 'Parts' (length=5)
      'EdPrice' => string '5' (length=1)
      'DateRepair' => string '2015-07-20' (length=10)
Sign up to request clarification or add additional context in comments.

2 Comments

I know the json is bad structured, but I don't find another solution to get value from inputs and send to php code and make records in the database. If you have better solution I will be happy to see it jsfiddle.net/1ox3Ltfr/1. Thanks.
Put your fields into a form, then when you click the show button do this: var formData = $("#formId").serialize(); You have send this to server via AJAX and then you will received a correct array, the you can convert to JSON or not
0

$query = mysqli_query($mysqli, "INSERT INTO repair (DescRepair, NestParts, EdPrice, DateRepair) VALUES ('?', '?', '?', '?')") or die(mysqli_error($mysqli))

relace each ? with {$arr['xxxxx']} Where xxxxx are your array keys

make sure to well-escape the variables to prevent SQL Injection

Hint: you can use PDO or prepared statements

Comments

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.