Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America asked on

How to insert this data in one new table

I am showing the data to my visitors by selecting it from different tables. Now what I want to do is to transfer this data into one table exactly as it is presented to the visitors. How can I do that. Please find my code below to show the data to visitors. Thanks
<?php include('column.php') ?>
<body>
<table width="1954" border="2">
  <tr>
    <td width="213">&nbsp;</td>
    <td width="248">Question1</td>
    <td width="289">Question2</td>
    <td width="289">Question3</td>
    <td width="289">Question4</td>
    <td width="289">Question5</td>
    <td width="289">Question6</td>
  </tr>
  <tr>
    <td><strong>17266408</strong></td>
    <td>
    <?php $qu = "Select 79666X451X4679 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?>    </td>
    <td><?php $qu = "Select 79666X451X4680 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 79666X451X4681 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 79666X451X4682 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 79666X451X4683 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 79666X451X4684 from survey_79666";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
  </tr>
  <tr>
    <td><strong>18704956</strong></td>
    <td><?php $qu = "Select 64468X447X4655 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 64468X447X4656 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 64468X447X4657 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 64468X447X4658 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 64468X447X4659 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
    <td><?php $qu = "Select 64468X447X4660 from survey_64468";
 
$result = mysql_query($qu);

while($row = mysql_fetch_array($result))
{
    echo "{$row[0]} <br>" ;
}

?></td>
  </tr>
  Thanks in advance
PHPMySQL Server

Avatar of undefined
Last Comment
Joe Wu

8/22/2022 - Mon
ASKER
syedasimmeesaq

of course the new table that I want has the fields Question1, Question2, Question3, Question4, Question5 and Question6.
I was hoping to use some kind of update query or insert query.

Thanks
Joe Wu

As an example something like this!

INSERT INTO new_table (Question1,Question2,Question3,Question4,Question5,Question6)
SELECT 79666X451X4679,79666X451X4680,79666X451X4681,79666X451X4682,79666X451X4683,79666X451X4684 FROM survey_79666;

You will pretty much have the same query for your second lot in your example.

Hope you understand my example?
ASKER
syedasimmeesaq

I tried to do this and this didn't insert any data
<?php
include('column.php');
$query = "INSERT INTO newdata (id,submitdate,startlanguage,q1,q2,q3,q4,q5,q6,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24,q25,q26,q27,q28,q29,q30,q31,q32,q33,q34,q35,q36,q37,q38,q39,q40,q41,q42,q43,q44,q45,q46,q47,q48,q49,q50,q51,q52,q53,q54,q55,q56,q57,q58,q59,q60,q61,q62,q63,q64,q65,q66,q67,q68,q69,q70,q71,q72,q73,q74,q75,q76,q77,q78,q79,q80,q81,q82,q83,q84,q85,q86,q87,q88,q89,q90,q91,q92,q93,q94,q95,q96,q97,q98,q99,q100,q101,q102,q103,q104,q105,q106,q107,q108,q109) SELECT * FROM survey_36145";
?>
Thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Joe Wu

um what are you trying to do?

your table should only have 6 questions so ultimately each row has 6 columns (thus the 6 questions) and inserting into the new table there should be a lot of rows with 6 columns only. What you are trying to enter is a ot of questions, but when you select * from survey_36145 there is only 6 columns.

I hope you undrstand what I am getting at.
ASKER
syedasimmeesaq

no survey_36145 has exactly same rows as the newdata table now. The 6 rows was for other question that we are working on. This one is 113 rows

Thanks
ASKER
syedasimmeesaq

I mean 113 columns
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
syedasimmeesaq

should the column number be mattered if survey_36145 has exactly same columns as newdata all it should do is put all the data that is in survey_36145 into the newdata.
right?
Thanks
ASKER
syedasimmeesaq

any idea?
Joe Wu

yes if the new table has the same amount of columns as the old table, all you have to do is:

INSERT into new_table SELECT * FROM old_table

As simple as that! :)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
syedasimmeesaq

I tried that. It didn't work
Joe Wu

The new and the old table STRUCTURE must be exactly the same, and yes, I tried it it does work so you must verify the structures are the same.

The easiest way is to create a new table like this:

CREATE TABLE new_table LIKE original_table;

then do the insert statement above:
INSERT into new_table SELECT * FROM old_table

All should work.
ASKER
syedasimmeesaq

ok the structure is exactly same. Only thing different(as you know) is the field names. I will recheck one more time and will let you know here shortly. The old table has 7 rows of data in it, do u think that could be a problem.
Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Joe Wu

The structure has to be the same, i assume including field names (column names) and the number of columns there are. Rows do not matter, but columns are the important ones.
ASKER
syedasimmeesaq

Nizmo the field names are different. Thats why I need a fix for that so I don't have to enter field names everytime. Do you think there is a solution for that.
Thanks
ASKER CERTIFIED SOLUTION
Joe Wu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question