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

  • SEARCH
  • Home
  • 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 7492075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:13:45+00:00 2026-05-29T16:13:45+00:00

I have 2 tables relating to a survey. When user answers each set of

  • 0

I have 2 tables relating to a survey. When user answers each set of questions and then click the submit button, it will loop each answer according to the form submitted in order to check within the database first, if the CustomerID and QuestionID have been found, then do the Update. If not found, do the Insert instead.

QUESTIONS table

  • QuestionID (PK)
  • QuestionText

ANSWERS table

  • AnswerID (PK)
  • CustomerID (FK)
  • QuestionID (FK)
  • AnswerText

    <html>
    ....
    <form action="/db.php" method="POST">
     <?php echo $questiontext[1]; ?><input type="text" name="answer1" id="answer1">
     <?php echo $questiontext[2]; ?><input type="text" name="answer2" id="answer2">
     <?php echo $questiontext[3]; ?><input type="text" name="answer3" id="answer3">
     <?php echo $questiontext[4]; ?><input type="text" name="answer4" id="answer4">
     <?php echo $questiontext[5]; ?><input type="text" name="answer5" id="answer5">
     <?php echo $questiontext[6]; ?><input type="text" name="answer6" id="answer6">
    
     <input type="submit" name="submit" id="submit" value="Submit">
    </form>
    ...
    </html>
    

db.php

    <?php
    if(isset($_POST['submit'])) {
      $cusid = intval($_POST['CustomerID']);
      $answer1 = $db->real_escape_string($_POST['answer1']);
      $answer2 = $db->real_escape_string($_POST['answer2']);
      $answer3 = $db->real_escape_string($_POST['answer3']);
      $answer4 = $db->real_escape_string($_POST['answer4']);
      $answer5 = $db->real_escape_string($_POST['answer5']);
      $answer6 = $db->real_escape_string($_POST['answer6']);

      $sql = "INSERT INTO Answers (CustomerID, QuestionID, AnswerText) VALUES     
      ('".$cusid."','".$quesid."','".$answer."')";
      $res = $db->query($sql) or die ('Error: ' . mysqli_error($db));
    }
    ?>

My questions are:

  1. How to update each answer(1-6) one by one and then insert into the database if CustomerID and QuestionID have not been found by using array and SQL query, if found, then just update?
  2. How could I reference the QuestionID in order to related with AnswerText in HTML and PHP?
  • 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-29T16:13:46+00:00Added an answer on May 29, 2026 at 4:13 pm

    This is just an idea for you. Hope you can understand it. Make sure you replace database driven functions with your $db object.

    if(isset($_POST['submit'])) {
        $sql = "SELECT QuestionID FROM Questions ORDER BY QuestionID ";
        $res = $db->query($sql);
        $qus = 1;
        while ($row = mysqli_fetch_array($res , MYSQLI_ASSOC)) {
        {
            $questionID = $row['QuestionID'] ;
            $answer = $db->real_escape_string($_POST['answer' . $qus ]);
    
    
            $sql = "SELECT AnswerID  FROM Answers WHERE CustomerID='$cusid' AND QuestionID='$questionID' ";
            $resAns = $db->query($sql);
            $num_rows = $resAns->num_rows; // This should be replace with your $db object record count obtaining method
            if($num_rows == 1)
            {
                $sql = "UPDATE Answers SET AnswerText = '$answer' WHERE CustomerID='$cusid' AND QuestionID='$questionID'  ";
                // Execute your update query
            }
            else
            {
                $sql = "INSERT INTO Answers (CustomerID, QuestionID, AnswerText) VALUES     
      ('".$cusid."','".$questionID."','".$answer."')";
                // Execute your insert statement
            }
            $qus ++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a personnel database and have multiple tables relating to a user
I have the following tables: Post Id int User Id int Then I have
i have 2 entities each with a relating c# class. I set up a
hi i have tables like this: Persons: person_id, name then i have many langauge
I have three tables that contain data relating to a Job. One table is
I have imported data relating to about 70 human subjects from three data tables
I have two tables User id name Result id user_id score created_at Every hour
I have a table with data relating to several moments in time that I
I have tables for a forum system. I am tring to get the following
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int

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.