警告:mysqli_prepare()期望参数1为mysqli,给定null

时间:2019-06-23 23:02:12

标签: php mysqli

我无法使用以下功能。

transaction.php

<?php 
require_once "config.php";

function transaction($sourceWallet, $destination, $currency, $amount) 
    {
        // Prepare an insert statement
        $sql = "INSERT INTO blockchain (sourceWallet, destination, currency, amount) VALUES (?, ?, ?, ?)";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ssss", $param_sourceWallet, $param_destination, $param_currency, $param_amount);

            // Set parameters
            $param_sourceWallet = $sourceWallet;
            $param_destination = $destination;
            $param_currency = $currency;
            $param_amount = $amount;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                header("location: ../admin-panel.php");
            } else{
                echo $transaction;
                 printf("Error: %s.\n", $stmt->error);
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);

        // Close connection
        mysqli_close($link);
    }
?>

以下正在执行交易功能 admin-wallet.php

if(isset($_POST['withdraw'])){
    include 'transaction.php';

    $currency = "cash";
    $destination = "Withdrawal";

    // Validate new password
    if(empty(trim($_POST["cash"]))){
        $cash_err = "Please amount of cash.";     
    }else{
        $cash = trim($_POST["cash"]);
    }

    // Validate confirm password
    if(empty(trim($_POST["walletID"]))){
        $walletID_err = "Please enter Wallet ID.";
    } else{
        $walletID = trim($_POST["walletID"]);
    }

    // Check input errors before updating the database
    if(empty($cash_err) && empty($walletID_err)){
        // Prepare an update statement
        $sql = "UPDATE wallets SET cash = cash - ? WHERE walletID = ?";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ds", $param_cash, $param_walletID);

            // Set parameters
            $param_cash = $cash;
            $param_walletID = $walletID;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                $amount = $cash;
                transaction($param_walletID, $destination, $currency, $amount);
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}

config.php

<?php
/* Database credentials. */
define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', '*********');
define('DB_PASSWORD', '*********');
define('DB_NAME', 'dabcoin');

/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

运行所有命令后出现这些错误。

  

警告:mysqli_prepare()期望参数1为mysqli,在第9行的C:\ inetpub \ vhosts \ nopixel.online \ httpdocs \ dabcoin \ action \ transaction.php中给出空值

     

警告:mysqli_stmt_close()期望参数1为mysqli_stmt,在第29行的C:\ inetpub \ vhosts \ nopixel.online \ httpdocs \ dabcoin \ action \ transaction.php中给出空值

     

警告:mysqli_close()期望参数1为mysqli,在第32行的C:\ inetpub \ vhosts \ nopixel.online \ httpdocs \ dabcoin \ action \ transaction.php中给出空值

0 个答案:

没有答案