Insert Data from one table to another table

Discussion in 'PHP' started by baris22, Jan 16, 2009.

  1. #1
    How can i Insert Data from one table to another table using php.

    Thanks
     
    baris22, Jan 16, 2009 IP
  2. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    yes.

    if you have table like:

    TABLE 1:
    ID | COLUMN_A | COLUMN_B
    1 | value1 VALUE2
    2 | value1 VALUE2

    TABLE 2:
    ID | COLUMN_A | COLUMN_B

    do you have previous knowledge of fetching data from mysql?
    you first need to fetch data from table 1 and then put in table 2, IDs work good here, if you have table with id, that would make the thing easy. First fetch the required row using WHERE id=1 or something.

    you can use something like this:
    
    // first fetch all the data and then use this thing
    $id = ""; // replace "" with your row id of table1
    $value_clm_a = ""; // value of first column
    $value_clm_b = ""; // value of first column
    
    $query = "INSERT INTO table2 ( COLUMN_A, COLUMN_B ) VALUES ( '{$value_clm_a}', '{$value_clm_b}' ) ";
    $result = mysql_query($query) or die(mysql_error());
    if($result) { echo "success"; } 
    else { echo "The values could not be entered"; }
    
    PHP:
     
    khan11, Jan 16, 2009 IP
  3. firmaterra

    firmaterra Peon

    Messages:
    756
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Are they on the same database?

    If so you can always do something like:

    mysql_query("INSERT INTO `TABLE_A` SELECT * FROM `TABLE_B`");
    PHP:
     
    firmaterra, Jan 16, 2009 IP