[Solved] How do I insert data from a json array into mysql database? [closed]


What have you tried?

Try this approach:

  • Convert JSON to PHP array (json_decode())
  • Loop through the array, get the key and value for each entry (foreach(){}, array_keys())
  • Create a single string with an insert and add VALUES() for each row
  • Execute the query after the loop

    $keys = array_keys($array);              // get the value of keys
    $rows = array();                         // create a temporary storage for rows
    foreach($keys as $key) {                 // loop through
        $value = $array[$key];               // get corresponding value
        $rows[] = "('" . $key . "', '" . $value . "')";
                                             // add a row to the temporary storage 
    }
    $values = implode(",", $rows);           // 'glue' your rows into a query
    $query = "INSERT INTO ... VALUES " . $values;
                                             // write the rest of your query
    ...                                      // execute query
    

As soon as you find a concrete question, feel free to open another post.

0

solved How do I insert data from a json array into mysql database? [closed]