调用准备/绑定值的值

时间:2016-01-17 01:19:52

标签: php mysql

我试图在表单提交的同时调用$userId值。我认为我唯一的选择是通过$stmt->bind_param,但我不知道如何调用该值。

    // Record a Payment Received
    if (isset($_POST['submit']) && $_POST['submit'] == 'recordPay') {
        // User Validations
        if($_POST['paymentDate'] == '') {
            $msgBox = alertBox($payDateReq, "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['paymentFor'] == '') {
            $msgBox = alertBox($payForReq, "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['amountPaid'] == '') {
            $msgBox = alertBox($payAmtReq, "<i class='fa fa-times-circle'></i>", "danger");
        } else if($_POST['paymentType'] == '') {
            $msgBox = alertBox($payTypeReq, "<i class='fa fa-times-circle'></i>", "danger");
        } else {
            $paymentDate = htmlspecialchars($_POST['paymentDate']);
            $paymentFor = htmlspecialchars($_POST['paymentFor']);
            $amountPaid = htmlspecialchars($_POST['amountPaid']);
            $paymentType = htmlspecialchars($_POST['paymentType']);
            $rentMonth = htmlspecialchars($_POST['rentMonth']);
            $rentYear = htmlspecialchars($_POST['rentYear']);
            $notes = htmlspecialchars($_POST['notes']);
            $propertyName = htmlspecialchars($_POST['propertyName']);
            $leaseId = htmlspecialchars($_POST['leaseId']);
            $userId = htmlspecialchars($_POST['userId']);

            if ($_POST['penaltyFee'] == '') { $penaltyFee = null; } else { $penaltyFee = htmlspecialchars($_POST['penaltyFee']); }
            if ($rentMonth == '...') {
                $isRent = '0';
                $rntMonth = null;
            } else {
                $isRent = '1';
                $rntMonth = $rentMonth;
            }
            if ($rentYear == '') {
                $rntYear = null;
            } else {
                $rntYear = $rentYear;
            }

            $stmt = $mysqli->prepare("
                                INSERT INTO
                                    payments(
                                        leaseId,
                                        propertyId,
                                        adminId,
                                        userId,
                                        paymentDate,
                                        amountPaid,
                                        penaltyFee,
                                        paymentFor,
                                        paymentType,
                                        isRent,
                                        rentMonth,
                                        rentYear,
                                        notes,
                                        lastUpdated,
                                        ipAddress
                                    ) VALUES (
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        NOW(),
                                        ?
                                        ()

            ");
            $stmt->bind_param('ssssssssssssss',
                $leaseId,
                $propertyId,
                $rs_adminId,
                $userId,
                $paymentDate,
                $amountPaid,
                $penaltyFee,
                $paymentFor,
                $paymentType,
                $isRent,
                $rntMonth,
                $rntYear,
                $notes,
                $ipAddress
            );
            $stmt->execute();
            $stmt->close();

I then want to take the `$userID` and associate it with another `userId` in the `users` table so I can echo the `primaryPhone` column in `users` table.  I only want to echo the `primaryPhone` from the data submitted on the form.

>1. Call `$userId` from `$stmt->bind_param`
>2. Associate the `$userId` with userId from `users` table to pull `primaryPhone`

编辑:这是我正在使用的代码及其加载页面,只是没有返回值。我将此代码放在$stmt->execute();

   if ($stmt->execute()){
                $qryPhone = mysqli_query("SELECT primaryPhone FROM users WHERE userId = '$userID'");
                echo $qryPhone;
                }

并加载页面,但不提供primaryPhone值。但是当我使用

if ($stmt->execute()){
            echo $userId;
            }

然后我得到了我需要的userId,但我无法用它做任何事情。

编辑:修复。

我将此代码放在$stmt->close();

之后
$qryPhone = "SELECT primaryPhone FROM users WHERE userId=".(int)$userId;
 $query  = mysqli_query($mysqli, $qryPhone);
while ($fetch = mysqli_fetch_array($query)) {
        $primaryPhone = '+1'.decryptIt($fetch['primaryPhone']);
        echo $primaryPhone;
    }

1 个答案:

答案 0 :(得分:0)

首先,mysqli_query()至少有两个参数,第一个是您的连接处理程序,第二个是您的查询,如下所示:mysqli_query($connection, $query)

以下是参考资料:

其次,不要混淆mysqli的程序和面向对象风格。

最后来到你的问题,首先准备语句,绑定必要的参数,执行查询,绑定结果,最后获取值。

以下是参考资料:

所以你的代码应该是这样的:

if ($stmt->execute()){
    // create a prepared statement
    $stmt = $mysqli->prepare("SELECT primaryPhone FROM users WHERE userId=?");

    // bind parameter
    $stmt->bind_param("s", $userID);

    // execute query
    $stmt->execute();

    // bind result variables
    $stmt->bind_result($primaryPhone);

    // fetch value
    $stmt->fetch();

    // echo primaryPhone
    echo $primaryPhone;
}