0

i am trying to send JSON data to PHP server, but i can understand how can i parse my JSON in PHP and insert values in Mysql database.

Here is how looks my JSON data

 {"reps_list":{"selected_subcategory_id":[0,1,2]}}

Here is my PHP code:

$reps_list = $_POST['reps_list'];
$reps_list= json_decode($reps_list,TRUE);

 for($i = 0; $i <= count($array['reps_list']['selected_subcategory_id']); $i++){
mysqli_query($conn, "INSERT INTO reps VALUES(NULL, '".$array['reps_list']['selected_subcategory_id'][i]."', 1, 1 )");

}

Any Suggestions?

4
  • 1
    it is not valid json, post you valid json Commented Mar 20, 2018 at 17:46
  • If that was valid JSON data then you'd use json_decode, but it's not so you can't. Commented Mar 20, 2018 at 17:47
  • i have edited my question and paste VALID json )) Commented Mar 20, 2018 at 17:50
  • You have an SQL injection vulnerability. You should use parameters in your query, instead of interpolating variables into your SQL string. json_decode() does not protect values. Commented Mar 20, 2018 at 17:54

3 Answers 3

2

Try like this for your valid json, decode the string as array using json_decode with second parameter true to make it array and then treat it as an array before using it on db query.

$array = json_decode('{"reps_list":{"selected_subcategory_id":[0,1,2]}}',1);
echo "Main Array after json string decode \n\n";
print_r($array); 
echo "\n\n";
echo "Access selected_subcategory_id \n\n";
print_r($array['reps_list']['selected_subcategory_id']);

Output:

Main Array after json string decode

Array
(
    [reps_list] => Array
        (
            [selected_subcategory_id] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                )

        )

)

Access selected_subcategory_id

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
)

DEMO: https://eval.in/975261

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

Comments

1

Since you've updated your post with the intention to INSERT it into the database, here is what will work:

$reps_list = $_POST['reps_list'];
$json = json_decode($reps_list,TRUE);

$arr = $json['reps_list']['selected_subcategory_id'];

$insertWorkout = $mysqli->prepare("INSERT INTO reps (column1, column2, column3) VALUES (?, ?, ?)");
$insertWorkout->bind_param("iii", $arr[0], $arr[1], $arr[2]); // iii means they are all integers
$insertWorkout->execute();

1 Comment

thanks for answer, have updated my question, i want to insert data in LOOP, but when i am running the code, throws exception like this Undefined index: i
0

You can use

foreach($reps_list['selected_subcategory_id'] as $key => $value){

mysqli_query($conn, "INSERT INTO reps VALUES(NULL, '".$value."', 1, 1 )");

}

2 Comments

throws this exception PHP Parse error: syntax error, unexpected '$key' (T_VARIABLE) in /home/asata/public_html/etsports/data/add_reps.php on line 28
you can try now

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.