php多查询数据库无结果

2izufjch  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(284)

我正在开发一个需要连接到数据库的android应用程序。我有android部分工作,但我有我的php脚本的麻烦。

<?php
$con = mysqli_connect(/This info is correct/) or die ("Unable to connect");

$gebruikersnaamOntvanger = $_POST["gebruikersnaamOntvanger"];
$idBetaler = $_POST["idBetaler"];
$bedrag = $_POST["bedrag"];
$saldoBetaler = $_POST["saldo"];
$response = array();
$response["success"] = "false";

$statement = mysqli_prepare($con, "SELECT idGebruiker, Saldo FROM Gebruikers WHERE Gebruikersnaam = ?");
mysqli_stmt_bind_param($statement, "s", $gebruikersnaamOntvanger);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $idGebruiker, $Saldo);

while($row = mysqli_stmt_fetch($statement)){
    $idOntvanger = $idGebruiker;  
    $saldoOntvanger = $Saldo;
}

$saldoOntvanger += $bedrag;
$saldoBetaler -= $bedrag;

try {

    $statement2 = mysqli_prepare($con, "INSERT INTO Transacties (idBetaler, idOntvanger, Bedrag, Datum, Uitgevoerd) VALUES(?, ?, ?, now(), 1)");
    mysqli_stmt_bind_param($statement2, "iid", $idBetaler, $idOntvanger, $bedrag);  
    mysqli_stmt_execute($statement2);

    $response["success"] = "success";

} catch(Exception $e) {
    $response["success"] = $e->gettext;
}

echo json_encode($response);
 ?>

所以android部分正常工作并正确返回json对象,但数据库中没有任何更改。我尝试添加了一个try-catch,所以我得到了错误所在,但是try-catch永远不起作用。脚本总是返回成功,即使它不工作。请注意,我是新来的php和我已经仔细检查了sql查询,他们应该是正确的。如果您需要更多信息,请询问。

aor9mmx1

aor9mmx11#

我认为问题在于 iid 在insert查询的绑定中。应该是 iis 或者 iii 大概是吧。你试过使用 try/catch 但在我的脑海中并没有充分利用它们——创造了一个 prepared statement 应该进行测试,看它是否成功或失败~其结果可用于 try/catch 指示故障的逻辑。请原谅我 procedural 以及 OOP 样式代码

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST["gebruikersnaamOntvanger"], $_POST["idBetaler"], $_POST["bedrag"], $_POST["saldo"] ) ){

        try{

            $con = mysqli_connect(/This info is correct/) or die ("Unable to connect");

            $gebruikersnaamOntvanger = $_POST["gebruikersnaamOntvanger"];
            $idBetaler = $_POST["idBetaler"];
            $bedrag = $_POST["bedrag"];
            $saldoBetaler = $_POST["saldo"];

            $response = array('success'=>false);

            $stmt=$conn->prepare( 'select `idgebruiker`, `saldo` from `gebruikers` where `gebruikersnaam` = ?' );
            if( !$stmt ) throw new Exception('unable to prepare select query');
            else {

                $stmt->bind_param( 's', $gebruikersnaamOntvanger );
                $result=$stmt->execute();
                if( !$result )throw new Exception('No results from select query');
                else {

                    $stmt->store_result();
                    $stmt->bind_result( $idGebruiker, $Saldo );

                    while( $stmt->fetch() ){
                        $idOntvanger = $idGebruiker;  
                        $saldoOntvanger = $Saldo;                       
                    }

                    $saldoOntvanger += $bedrag;
                    $saldoBetaler -= $bedrag;                   

                    $stmt=$con->prepare('insert into `transacties` ( `idbetaler`, `idontvanger`, `bedrag`, `datum`, `uitgevoerd` ) values(?, ?, ?, now(), 1)');
                    if( !$stmt )throw new Exception('Unable to prepare insert query');
                    else {

                        /* What is the value of $bedrag? integer, string?? */
                        /*
                            The binding iid was incorrect - perhaps iis or iii
                        */
                        $stmt->bind_param('iis', $idBetaler, $idOntvanger, $bedrag );
                        $result = $stmt->execute();
                        $response['success']=$result;

                    }
                }
            }

            echo json_encode( $response );

        }catch( Exception $e ){
            echo $e->getMessage();
        }
    }
?>
zi8p0yeb

zi8p0yeb2#

mysqli函数不抛出异常。所以即使有错误,你的捕获也不会被执行。更改代码如下:

if(!mysqli_stmt_execute($statement2))
         throw new Exception(mysqli_stmt_error($statement2));

当您看到错误时,可以修复代码。可能会添加相同的错误处理mysqli\u prepare和其他

相关问题