How to insert nested arrays in MySQL using PHP

I have an array that represents a quiz created by my app. Here I've got the question title, type of question and it's answers in a nested array.

Here's an example of the array:

Array
(
    [0] => stdClass Object
        (
            [options] => Array
                (
                    [0] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 1
                        )

                    [1] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 2
                        )

                    [2] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 3
                        )

                )

            [content] => Question 1
            [type] => radios
        )

    [1] => stdClass Object
        (
            [options] => Array
                (
                    [0] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 1
                        )

                    [1] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 2
                        )

                    [2] => stdClass Object
                        (
                            [valid] => 
                            [content] => Answer 3
                        )

                )

            [content] => Question 2
            [type] => radios
        )

)

Right now my code looks like this:

$name = $_POST['name'];
$test = json_decode($_POST['test']);

$con=mysqli_connect("Localhost","root","","test");

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

This covers the connection to the database.
While doing my research I found this question. It covers the basic principles of the insert process, but I have a problem while trying to implement the solution:

I have two tables. A question table and an options table. The nested array 'options' in my data needs to be inserted in the options table and each one of its positions referenced to the main question. How can I do this, while inserting the 'content' and the 'type' fields into the question table?

EDIT: I tried this code but it's currently not working. How could I adapt it?

$sql = "INSERT INTO question (content, type) values ";

$valuesArr = array();
    foreach($test as $row){

    $content = mysql_real_escape_string( $row['content'] );
    $type = mysql_real_escape_string( $row['type'] );

$valuesArr[] = "('$content', '$type')";
}

$sql .= implode(',', $valuesArr);

EDIT: My table structure:

Question

Options

Upvotes: 0

Views: 763

Answers (1)

sven
sven

Reputation: 785

Your table structure is:

Question Options

ID (PK) ID (PK)

Content Question_ID (FK)

Type Content

I've taken the whole array as $questions (the object you pasted in your question)

foreach( $questions as $question ){
  $options = $question->options;
  $options_id = array();
  $question_query = "INSERT INTO questions (Content, Type) VALUES ({$question->content}, {$question->type})";
  if( !mysqli_query($con, $question_query) ){ echo "Query failed"; return; }
  $question_id= mysqli_insert_id();
  foreach($options as $option){
      $options_query = "INSERT INTO options (Question_ID, Content, Valid) VALUES ({$question_id}, {$option->content}, {$option->valid})";
      if( mysqli_query($con, $options_query) ){
       $options_id[] = mysqli_insert_id();
      }
  }

  if($options_id){
    //Do something or store options and question id
    var_dump($options_id); //just to check
  }
}

Upvotes: 1

Related Questions