2016-08-04 58 views
0

在那里我想更新和创建一个条件,所以当我创建一个新的记录,自动更新我的数据,如果我有成功使新的。PHP插入和更新多个查询和表

我在这里的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)); 
} 

我的查询:$query$sql

我希望我的$sql它的更新时$query是成功创建

+1

你永远不会运行$查询,你从来没有使用所有$ a数组你创建的变量,你打开sql注入。除了那个im甚至不知道什么是 – 2016-08-04 03:45:41

+0

MySql <> Sql Server。 –

+0

请参阅插入..在重复键上。 – Strawberry

回答

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

创建一个存储过程插入然后更新。你可能想要做这样的事情来让你远离发出常规查询来检查子查询并将你转移到创建一个存储过程。

创建你的程序到下面类似的东西,并在你的sql对话框中运行它。一旦您完成后,运行它:

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; 

现在,在你的PHP文件,请执行下列操作:

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 ****/