0

In my database I have the following schema:

Answers:

answerId(PK) auto_inc
answer
questionId

I am passing the following JSON String to my php file:

[{"answer":"bnk","questionId":"1"},{"answer":"1","questionId":"2"},{"answer":"b n","questionId":"3"},{"answer":"3","questionId":"4"},{"answer":"rgb","questionId":"5"},{"answer":"No","questionId":"6"},{"answer":"0","questionId":"7"},{"answer":"0","questionId":"8"},{"answer":"0","questionId":"9"},{"answer":"0","questionId":"10"},{"answer":"0","questionId":"11"},{"answer":"0","questionId":"12"},{"answer":"0","questionId":"13"},{"answer":"0","questionId":"14"},{"answer":"3","questionId":"18"},{"answer":"nko","questionId":"19"},{"answer":"hhkl","questionId":"15"},{"answer":"2","questionId":"16"},{"answer":"vnlf hugg","questionId":"17"}]

This is captured via a post request in $_POST['answers']:

if(isset($_POST['submitanswer'])){
  $dbh = connect();
  $user = $_POST['user'];
  $entry = $_POST['entryId'];
  $answers = $_POST['answers'];
  $answers = json_decode($answers); //decode JSON answers

  //for loop to iterate through answers ans insert new row into database
}

How do I iterate through the answers array and insert a new row into my answers table?

Something like:

foreach($answers as $row){
   $query = "INSERT INTO Answers (answer, questionId) VALUES ($row['answer'], $row['questionId'])";
   mysql_query($query);
}

2 Answers 2

1

If this code didn't work for you, try this:

foreach($answers as $row){
 $query = "INSERT INTO Answers (answer, questionId) VALUES (".$row['answer'].", ".$row['questionId'].")";
 mysql_query($query); 
}

Otherwise, I can't spot anything wrong here.

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

Comments

0

I gues you know this but make sure your connection string is good.

Actually this is what I do. Probably a bit much info for you, also I do all that concatenation in the SQL so I can easily comment out fields for testing.

$Link = mysql_connect( $Host , $User , $Password , $DBName);

if (!$Link) {
    die('Could not connect: ' . mysql_error());
}



$sql = "insert into table "
                ."("
                    ."hashfirstName".","
                    ."hashfamilyName".","
                    ."hashemailAddress"
                    .")"
                ."values ("
                    ."'$firstNameHashed'".","
                    ."'$familyNameHashed'".","
                    ."'$emailAddressHashed'"
                    .")";

mysql_select_db($DBName , $Link) or die("Database error in insertdata<br>"."Error #" . mysql_errno() . ": " . mysql_error());



            if(!mysql_query($sql , $Link))
            {
                $errors['sql'] = $sql; 
                $errors['DBName'] = $DBName;
                $errors['Link'] = $Link;
                $errors['status'] = "false"; //There was a problem saving the data;
                echo json_encode($errors);
            }
            else
            {
                $errors['status'] = "true";
                echo json_encode($errors);
            }; // if(!mysql_query( $DBName , $sql , $Link))

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.