In MySQL, how to copy rows of two tables within the same table respectably using PHP?

Raag Singh

I have two tables questions and answers and would like to copy the rows of each table into the same table using PHP. The structure of the tables are as follows:

Table: questions

enter image description here

Table: answers

enter image description here

Currently I am using following code but it is not working:

include "db.php";    
$exam_title=$_POST["title_id"];
$exam_id=$_POST["exam"];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "insert into questions(question, option_type, media_type, media_link, exam_id)
    select question, option_type, media_type, media_link, '$exam_id' from questions where exam_id = ". $exam_id;   

$sql2 = "insert into answers(answer, ques_id, right_ans)
    select a.answer, q.ques_id, a.right_ans from questions q, answers a where q.exam_id = ". $exam_id;

if ($conn->query($sql) === TRUE) {
    echo "done";    
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}    
if ($conn->query($sql2) === TRUE) {
    echo "done2";    
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}    
$conn->close();

The issue is ques_id of the questions table and ans_id of answers table is primary key and is auto increment where ques_id of answers and ques_id of questions have relation.

Axel Amthor

If you are simply copyiing records in both tables which have a 1:1 relationship. and "questions" obviously is the master, you have new questions, all showing to the existing answers (i.e. two questions showing to just one single answer) and "dangling" answers not having any questions.

From my perspective, having a question twice but only one answer is what I call "normalization", but anyway, if you want to copy the entire set, you need (pseudo code)

Get all fields for all questions and their related answers (simple join)
for each row
   insert new question row
   get last insert id
   insert answer, set answer.ques_id to last insert id of question

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find and show number of similar words in two different column within same table(MySql,PHP)

From Dev

How to compare two tables using PHP and MySQL

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

How to select rows within a table that are not in other tables

From Dev

How to delete rows from two tables using INNER JOIN in mysql?

From Dev

Merge two tables to a new table using php and mysql

From Dev

How to return data from two tables only when the column value in one table is same as another table using MySQL?

From Dev

PHP pull from two MySQL tables, where multiple rows in table two

From Dev

PHP and MySql - fetching rows from two tables

From Dev

joining two tables and produce the table with same value in two different rows

From Dev

PHP/MySQL INSERT same data INTO two Tables

From Dev

PHP/MySQL INSERT same data INTO two Tables

From Dev

Select rows from a table based on results from two other tables in mySQL using PDO

From Dev

php mysql If I do not have the same data between two tables, can I print the other table data?

From Dev

Display MySQL table ( number of rows are not same ) in PHP

From Dev

how to merge two mysql tables with same structure

From Dev

Select rows from two different tables but order by date using php and MySQL

From Dev

mysql How to Find Difference between two rows in same table and list the Unmatched Records? mysql finding unmatched rows in a table

From Dev

Data cross matching with mysql two tables, value display in table using PHP mySql

From Dev

how to fetch a number of rows...from table in mysql using php

From Dev

MySQL join two tables using php

From Dev

Insert rows into a table from another two tables separately in mysql

From Dev

Copy table into two new tables

From Dev

How can I select multiple rows, all having a same id (or any variable) within a database using MySQL?

From Dev

PHP mysql how to join two tables to link name from one table and post from other table

From Dev

How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

From Dev

How can I union all rows of two different tables using MySQL 8.0.17 version?

From Dev

MySQL/Eloquent belongsToMany relation - two tables same pivot table allowed?

From Dev

Mysql: using two foreign keys to the same table

Related Related

  1. 1

    How to find and show number of similar words in two different column within same table(MySql,PHP)

  2. 2

    How to compare two tables using PHP and MySQL

  3. 3

    MYSQL: How to insert rows in a table based on a condition of other two tables

  4. 4

    How to select rows within a table that are not in other tables

  5. 5

    How to delete rows from two tables using INNER JOIN in mysql?

  6. 6

    Merge two tables to a new table using php and mysql

  7. 7

    How to return data from two tables only when the column value in one table is same as another table using MySQL?

  8. 8

    PHP pull from two MySQL tables, where multiple rows in table two

  9. 9

    PHP and MySql - fetching rows from two tables

  10. 10

    joining two tables and produce the table with same value in two different rows

  11. 11

    PHP/MySQL INSERT same data INTO two Tables

  12. 12

    PHP/MySQL INSERT same data INTO two Tables

  13. 13

    Select rows from a table based on results from two other tables in mySQL using PDO

  14. 14

    php mysql If I do not have the same data between two tables, can I print the other table data?

  15. 15

    Display MySQL table ( number of rows are not same ) in PHP

  16. 16

    how to merge two mysql tables with same structure

  17. 17

    Select rows from two different tables but order by date using php and MySQL

  18. 18

    mysql How to Find Difference between two rows in same table and list the Unmatched Records? mysql finding unmatched rows in a table

  19. 19

    Data cross matching with mysql two tables, value display in table using PHP mySql

  20. 20

    how to fetch a number of rows...from table in mysql using php

  21. 21

    MySQL join two tables using php

  22. 22

    Insert rows into a table from another two tables separately in mysql

  23. 23

    Copy table into two new tables

  24. 24

    How can I select multiple rows, all having a same id (or any variable) within a database using MySQL?

  25. 25

    PHP mysql how to join two tables to link name from one table and post from other table

  26. 26

    How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

  27. 27

    How can I union all rows of two different tables using MySQL 8.0.17 version?

  28. 28

    MySQL/Eloquent belongsToMany relation - two tables same pivot table allowed?

  29. 29

    Mysql: using two foreign keys to the same table

HotTag

Archive