我有一个用来获得报酬的脚本。我希望在事务状态返回“成功”时运行SQL查询。
我该怎么做?
使用以下代码检查交易状态:
checktransaction.php
<?php
require_once("class.php");
header('Content-Type: text/html; charset=utf-8');
$idealSEPA = new Ideal_SEPA();
$transactionID = $_GET["trxid"];
$requestTransactionStatus = $idealSEPA->requestTransactionStatus($transactionID);
echo '<pre>';
var_dump($requestTransactionStatus);
echo '</pre>';
?>
var_dump返回以下内容:
array(10) {
["result"]=>
string(7) "Success"
}
我使用以下方法:
public function requestTransactionStatus($transactionID){
if($transactionID == ""){
return false;
}
try {
$response = $this->iDEALConnector->getTransactionStatus($transactionID);
$this->status = $response->getStatus();
return array('status' => true, 'result' => $this->status);
} catch (SerializationException $ex){
return array('status' => false, 'error_msg' => $ex->getMessage());
}
}
我想知道如何检查transactionstatus是否“成功”并构建一个SQL查询,如果transactionstatus为“success”,则将数据插入数据库
更新1:
以下不起作用 返回数组( 'status'=&gt;真正, 在session_start();
$link = mysqli_connect("localhost", "root", "", "db");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "UPDATE payment SET pay='2' WHERE id='1'";
if(mysqli_query($link, $sql)){
echo " ";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
'result' => $this->status
);
答案 0 :(得分:0)
您可以在class.php中返回成功之前立即运行SQL查询。实际上,您可以检查以确保在返回成功之前没有SQL错误(在运行查询之后)。
编辑:根据您的新信息,您似乎正在尝试这样做:
public function requestTransactionStatus($transactionID){
if($transactionID == ""){
return false;
}
try {
$response = $this->iDEALConnector->getTransactionStatus($transactionID);
$this->status = $response->getStatus();
return array('status' => true,
session_start();
$link = mysqli_connect("localhost", "root", "", "db");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "UPDATE payment SET pay='2' WHERE id='1'";
if(mysqli_query($link, $sql)){
echo " ";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
'result' => $this->status);
} catch (SerializationException $ex){
return array('status' => false, 'error_msg' => $ex->getMessage());
}
}
如果您尝试在数组构造函数中执行数据库代码,那么它将不起作用。您需要做的是首先执行数据库代码,然后根据数据库查询的结果有条件地返回。下面的代码尚未经过测试,但它可以更好地表示代码在结构上应该是什么样的:
public function requestTransactionStatus($transactionID){
if($transactionID == ""){
return false;
}
try {
$response = $this->iDEALConnector->getTransactionStatus($transactionID);
$this->status = $response->getStatus();
session_start();
$link = mysqli_connect("localhost", "root", "", "db");
if($link === false) {
// Instead of dying here, you could even return an error like you do for serialization errors
return array('status' => false, 'error_msg' => "ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "UPDATE payment SET pay='2' WHERE id='1'";
if(mysqli_query($link, $sql)){
return array('status' => true, 'result' => $this->status);
} else{
// See my comment above
return array('status' => true, 'result' => "ERROR: Could not able to execute $sql. " . mysqli_error($link));
}
} catch (SerializationException $ex){
return array('status' => false, 'error_msg' => $ex->getMessage());
}
}
答案 1 :(得分:0)
我相信您应该在回复后插入状态并检查响应是否成功。
<?php
require_once("class.php");
header('Content-Type: text/html; charset=utf-8');
$idealSEPA = new Ideal_SEPA();
$transactionID = $_GET["trxid"];
$requestTransactionStatus = $idealSEPA->requestTransactionStatus($transactionID);
if(!empty($requestTransactionStatus['result'])){
if($requestTransactionStatus['result'] == "Success"){
// Connect to the database
$link = mysqli_connect("localhost", "root", "", "db") or die("ERROR: Could not connect. " . mysqli_connect_error());
$sql = "UPDATE payment SET pay='2' WHERE id='1'";
if(mysqli_query($link, $sql)){
echo " Payment status successfully updated. ";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}
}
?>