I need to insert data in to two tables in a same database at the same time. One will be editable where i will remove stock but one will be history where all data stays intacted.

Is it possible to do it?

Member Avatar

THis is duplication of data, you very rarely need to do something like this. This suggests that your tables aren't set up properly. A history table should link to the 'editable' table via an 'id'.


This should not be too hard to do. You can start by writing a simple function to do the db insert, and then call this function or method whenever there is something to be inserted.

for example, we can create something like this..* WARNING! Script below is not tested. I just made this up to give you the general idea.*

    function insert_to_db($query,$filter,$table){
    $dbConnect = mysql_connect("localhost", "dBUserName", "$dbPassword");

    ## $filter is something that will trigger or sort types of events so that we can actually execute the query if any or one of the set condition/s has been met.
    #$db is the database connection

    if($filter = 'condition 1'){

    ## add your error checking and connection failed below



    ## if you only have two $filters then else is appropriate to use. However, for multiple $filter, use else if or if else.

    ## add your error checking and connection failed below


    ## you can either return it true or say something to acknowledged the transaction  OR you can return from within the if and else statements.
    return true;


To use the function you will have provide the database query. Let's use the above function assuming that it has been tested and error free.

    $tableOne_query = "INSERT INTO Table_ONe(col_one, col_two, col_three) VALUES ('value_one',value_two','value_three')";

    $tableTwo_query = "INSERT INTO Table_Two(col_one, col_two, col_three) VALUES ('value_one',value_two','value_three')";

    ## now we can call our function above to do the dirty work..

    $insertTo_table_One = insert_to_db($tableOne_query,'condtion 1', 'Table_ONe');

    ## you can make a checkpoint here to see if the query One is successful , and if does execute the second query. For smaller apps this is pretty functional I think.

    $insertTo_table_Two = insert_to_db($tableTwo_query,'condtion 2', 'Table_Two');

I need this to insert all data in to one database that will be managed (add,remove parts) and one database which will not be editable and will record compete input history.

Thank you my friend :))