2016-06-01 34 views
0

所以我使用mysqli之前我的查詢,現在我將它轉換爲mysqli編寫。我試圖用上傳圖像更新特定的數據,我不知道爲什麼我得到的錯誤mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in line 30以及我怎樣才能在mysqli查詢中執行查詢如mysqli_query($conn, $query)如何使用mysqli準備數據庫更新?

以下是我的更新查詢的代碼:

if (isset($_POST['submit'])) { 

    $imageName = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["name"]); 
    $imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["latest_photo"]["tmp_name"])); 
    $imageType = mysqli_real_escape_string($conn, $_FILES["latest_photo"]["type"]); 

     if (substr($imageType, 0,5) == "image") { 

      $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?"; 
      $stmt = mysqli_prepare($conn, $query); 
      mysqli_stmt_bind_param($stmt, 'ss', $imageData, $_GET['id']); 
      mysqli_stmt_execute($stmt); 
      mysqli_stmt_bind_result($stmt, $id, $updated_photo);  

      //HOW CAN I EXECUTE THE QUERY HERE? 
      echo "Image Uploaded"; 

     } 

     else { 

      echo "Image is not uploaded!"; 

     } 

} 

在上面的代碼中,有關於如何執行查詢的註釋行。我怎樣才能做到這一點?感謝你們

編輯:

此外,當我點擊上傳按鈕,它說,圖像上傳,但不會出現在數據庫中。這是爲什麼?

+0

在我的'mysqli_stmt_bind_result()',我試圖包括該表中的所有列。 –

+0

您正在運行不返回任何結果的更新語句,這就是您無法使用'mysqli_stmt_bind_result'綁定結果的原因。如果你想要返回結果,那麼你將需要第二條語句,如'SELECT crew_info,updated_photo WHERE id =?' –

+0

對不起,我是mysqli新手準備的 –

回答

0
//for procedural way 
    $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?"; 
    $stmt = mysqli_prepare($conn, $query); 
    // you should use i instead of s for id 
    mysqli_stmt_bind_param($stmt, 'si', $imageData, $_GET['id']); 
    mysqli_stmt_execute($stmt); 

//try this out in object oriented 
    $query = "UPDATE `crew_info` SET `updated_photo` = ? WHERE `id` = ?"; 
    $stmt = mysqli_prepare($conn, $query); 
    $stmt->bind_param("si", $imageData, $id); 
    $imageData=$imageName ; 
    $id=$_GET['id']; 
    $stmt->execute(); 
+0

讓我知道如果它不適合你 –

+0

如何在程序中做? –

+0

好吧,我會改變我的答案等一下 –