2016-01-17 16 views
0

我試圖在表單提交的同時調用$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; 
    } 
+0

準備語句,綁定參數,執行並得到結果。這麼做阻礙了你?有什麼問題? –

+0

讓我難以忍受的是語法,因爲每當我嘗試這樣做時,腳本就會中斷。 –

+0

請解釋*腳本中斷*。它顯示任何錯誤? –

回答

0

首先,mysqli_query()需要ATLEAST兩個參數,第一個是你的連接處理器和第二個是你的查詢,例如: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; 
}