Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

  • ID (PK)
  • Content
  • Type

Options

  • ID (PK)
  • Question_ID (FK)
  • Content
share|improve this question
    
What have you tried for inserting? –  user2936213 Dec 30 '13 at 5:19
    
@user2936213 Edited. –  Jean Carlos Suárez Marranzini Dec 30 '13 at 5:24
    
Your Question and Options Table structure?, you want to store ids from option table to respective question? –  sven Dec 30 '13 at 5:28
    
@boom_Shiva I want to reference the options to its questions by ID. If there is a better way to do this, let me know please. Edited question. –  Jean Carlos Suárez Marranzini Dec 30 '13 at 5:35
    
Aren't you storing the value for Valid field for Options, the one there in your Object?, I've updated the answer –  sven Dec 30 '13 at 5:46

1 Answer 1

up vote 1 down vote accepted

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
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.