Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:59:39+00:00 2026-05-24T09:59:39+00:00

In my database I have the following schema: Answers: answerId(PK) auto_inc answer questionId I

  • 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:59:41+00:00Added an answer on May 24, 2026 at 9:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no control over database schema and have the following (simplified) table structure:
I have a database schema that is similar to the following: | User |
I have following data to save in database using php my data is :
I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have the following database schema : Store StoreId Name ... Order OrderId StoreId
I have the following database schema: Here's how I populate a DropDownList in my
I have a database with the following schema: ID PositionId LeagueId 1 4 5
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have the following database schema: table courses: id tutor_id title table course_categories: id
I have created Rails database using the following schema: ActiveRecord::Schema.define(:version => 20090807141407) do create_table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.