Link to home
Start Free TrialLog in
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
Avatar of syedasimmeesaq
syedasimmeesaq
Flag of United States of America image

ASKER

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
Avatar of 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?
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
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.
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
I mean 113 columns
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
any idea?
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! :)
I tried that. It didn't work
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.
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
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.
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
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial