2014-11-17 83 views
0

我找不到任何錯誤。我試着查詢phpmyadmin,它運作良好,但當我在PHP頁面做的時候,它不能更新到數據庫。下面的下面的代碼:更新成功但無法更新到數據庫

$registerID = ($_POST['registerID']); 
$firstName = ucwords(htmlspecialchars($_POST['firstName'])); 
$lastName = ucwords(htmlspecialchars($_POST['lastName'])); 
$emailAddress = htmlspecialchars($_POST['emailAddress']); 
$mainAddress = ucwords(htmlspecialchars($_POST['fullAddress'])); 
$updateCity = ucwords($_POST['userCity']); 
$updateCountry = $_POST['userCountry']; 
$postalCode = strtoupper(htmlspecialchars($_POST['userZip'])); 
$profilePic = $_POST['pic']; 

$updateProf = " UPDATE register_user 
       SET firstName='$firstName', 
        lastName='$lastName', 
        emailAddress='$emailAddress', 
        mainAddress='$mainAddress', 
        registerCity='$updateCity', 
        registerCountry='$updateCountry', 
        postalCode='$postalCode' 
       WHERE registerID = '$registerID'"; 

if (mysqli_query($mysqli, $updateProf)) { 

     echo "Record updated successfully"; 

     } else { 

     echo "Error updating record: " . mysqli_error($mysqli); 
} 

最後,有沒有錯誤,我更新了網頁後,它只是顯示記錄成功更新。但它沒有更新到數據庫。有任何想法嗎?

修訂CODING

$checkProfile = "SELECT * FROM register_user where emailAddress = '$emailAddress'"; 
$editProfile = mysqli_query($mysqli,$checkProfile); 

if ($editProfile) { 

    if (mysqli_num_rows($editProfile) > 0) { 

      header("Location: event?error=That name of email has already been taken"); 

      } else { 

      $updateQuery = "UPDATE register_user 
        SET firstName = '$firstName', 
         lastName = '$lastName', 
         emailAddress = '$emailAddress', 
         mainAddress = '$mainAddress', 
         registerCity = '$updateCity', 
         registerCountry = '$updateCountry', 
         postalCode = '$postalCode' 
         WHERE registerID = '$registerID'"; 

      $updateResult = mysqli_query($mysqli,$updateQuery); 

      header("Location: profileUser"); 

      } 
} 

我更新後,它仍然沒有我使用事先準備好的聲明後工作。有任何想法嗎?

+0

'它沒有插入DB'?你做了更新,爲什麼要插入? – Ghost

+0

對不起,我的意思是更新.. – Anthosiast

+0

不是你的問題的答案,但我認爲你打開SQL查詢與該查詢...嘗試參數化您的查詢,而不是... – mlinth

回答

1

嘗試先執行查詢,並將其保存到變量中。 然後,檢查查詢做執行:

if(!$query) echo "Query error : " . $mysqli->error; 

這會給你更詳細的錯誤報告。

+0

我試過這個,它顯示'查詢錯誤:'。 :/ – Anthosiast

+0

如果您嘗試直接在phpmyadmin中執行查詢,而不是使用變量但是使用示例數據,它是否工作? – McBurgerKong