-1

Is it possible to edit and save data as coded below simultaneously to two different tables? Just look at the code which enters data to the data tables.

        if($opt>=$rec['gwstart'] && $opt<=$rec['gwend'])
        {


        $con=mysql_connect('localhost','root') or die ("Server connection failure!"); 
        $db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the  database"); 
        $SQL22="UPDATE newchk SET totgw=$AAA where gwstart=$BBB"; 
        $run22=mysql_query($SQL22,$con) or die ("SQL Error"); 

        $SQL33="INSERT INTO invoentry (user_inv,dist_inv,chkNum,InvoNum,InvoVal,InvoDate,DueDate,type,condition) VALUES ('$naMex','$distUsr','$listVal','$InNo','$InValu','$date1','$date2','$typ','$cnd')"; 
        $run33 = mysql_query($SQL33,$con) or die ("SQL Error!..");
        $MSG ="Successfully saved your entry! ".$AAA." cheques are remaining.";
        }    
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Amw Amw
  • 43
  • 1
  • 8
  • 1
    Sure. Have you experienced any specific problems? – deceze Oct 15 '12 at 16:10
  • 2
    Use a transaction. Execute both of your queries within it. http://stackoverflow.com/questions/2708237/php-mysql-transactions-examples – Louis Ricci Oct 15 '12 at 16:11
  • 1
    Of course you can. As @LastCoder mentioned, transactions would be preferential in this case to ensure data integrity. – trickyzter Oct 15 '12 at 16:11
  • @deceze Yes. I can't save '$cnd' value to 'condition' and gives a SQL error message. I checked for my ' – Amw Amw Oct 15 '12 at 16:15
  • Oh thanks I figured out an error with a database field. Anyways I changed to transaction method as you all described. Thank you. – Amw Amw Oct 15 '12 at 16:27
  • Use `or die(mysql_error())` if you don't feel like stabbing into the dark. – deceze Oct 15 '12 at 16:38

1 Answers1

0

Yes you can, if your queries are somehow related and you need to make sure they both succeed you can use SQL TRANSACTIONS. otherwise, as you already did, you can run multiple queries.

Menthas
  • 26
  • 3