PHP在多个查询和表上插入和更新

in there i want to make update and create on one condition,so when I create a new record, automatically update my Data if have i success make new.

here my PHP :

<?php 
require "dbconnection.php";

$a = array();
$a['transidmerchant'] = $_POST['TRANSIDMERCHANT'];
$a['totalamount'] =$_POST['AMOUNT'];
$a['words'] = $_POST['WORDS'];
$a['payment_channel'] = $_POST['PAYMENTCHANNEL'];
$a['session_id'] = $_POST['SESSIONID'];
$a['payment_date_time'] = $_POST['REQUESTDATETIME'];
$a['trxstatus'] = 'Requested';
$query = "INSERT INTO doku (transidmerchant,totalamount,words,payment_channel,session_id,payment_date_time,trxstatus) 
VALUES ('$_POST[TRANSIDMERCHANT]','$_POST[AMOUNT]','$_POST[WORDS]','$_POST[PAYMENTCHANNEL]','$_POST[SESSIONID]','$_POST[REQUESTDATETIME]','Requested')";

$sql = "UPDATE orders SET status='Paid' where id='$_POST[TRANSIDMERCHANT]'";
if(mysqli_query($con,$query)) {
    mysqli_connect($con,$sql);
    echo 1;
}else{
    echo("Error description: " . mysqli_error($con));
}

my query : $query and $sql

i want my $sql its update when $query is success create

if (mysqli_query($con, $query) === true) {
    mysqli_query($con, $sql);
    echo 1;
} else {
    echo('Error description: ' . mysqli_error($con));
}

Create a stored procedure for insert then update. You may want to do something like this to get you away from issuing regular queries checking sub-queries and move you to creating a stored procedure.

Create your procedure to something similar below and run it in your sql dialog. Once you're done, run it:

DELIMITER //
CREATE PROCEDURE Payment
(
a_transidmerchant int,
a_atotalamount float,
a_words varchar(200),
a_payment_channel varchar(200),
a_session_id int,
a_payment_date_time datetime,
etc...
)
BEGIN
insert into doku(field_name1, field_name2, field_name3, field_name4) values(a_field1, a_field2, a_field3, a_field4);
END //
DELIMITER;

Now, in your php file, do the following:

if(isset($_POST[transidmerchantid])) /**** start a post check ****/ 
                                     //before you touch the db
{
 $con = mysqli_connect("localhost","user","pass","database");

 //start defining variables
 $transidmerchantid = $_POST[name];
 $totalamount = $_POST[course];
 $words = $_POST[words];

 //calling stored procedure - call values for parameters in stored procedure
 $sql = "CALL Payment('$transidmerchantid','$totalamount','$words')"; // <----

 //in the order of operation, meaning once you have inserted the data, 
 //you can update the table. you're automatically updating the table row 
 //based on a successful insert, which is after calling the insert row           
 //stored procedure. 

 $result = mysqli_query($con,$sql);
 if($result) //insert successful.
    echo "Record Added Successfully!";
    $sql = "UPDATE orders SET status='Paid' where id='$_POST[TRANSIDMERCHANT]'";
    mysqli_query($con,$query);
 }else{
    echo("Error description: " . mysqli_error($con));
 }else{ 
    echo "Record Not added!"; //insert unsuccessful.
 } 

} /**** end post check ****/