2015-10-08 271 views
0

我有頁面,用戶可以更新或更改他的個人信息,例如他的位置或他的個人資料圖片。默認情況下,每個用戶在其個人資料頁面上獲取佔位符數據或信息,並且mysql數據庫中的這些字段爲空或具有該用戶標識的行不存在。因此,首先我要檢查用戶是否先前將他的個人資料信息輸入到數據庫中,如果存在數據,我將僅使用非空的輸入字段進行更新,並且這部分工作正常。但是,如果數據不存在於數據庫或行中,並且此用戶標識不存在,那麼我想要插入數據,但只能使用輸入不爲空從非空輸入插入數據到MySQL數據庫用PHP

但是,這是錯誤我得到

未捕獲的異常「PDOException」有消息「SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤; ...

這是我的代碼($userID是會話$userID = $_SESSION['ID'];

if(isset($_POST['updateInfo'])) { 
$userDesc = filter_var($_POST['description_update'], FILTER_SANITIZE_STRING); 
$userLocation = filter_var($_POST['location_update'], FILTER_SANITIZE_STRING); 
$userBirthDate = filter_var($_POST['birthDate_update'], FILTER_SANITIZE_STRING); 
$user_picture_tmp = $_FILES['user_picture']['tmp_name']; 
$user_picture_name = $_FILES['user_picture']['name']; 


$checkInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userID"); 
$checkInfo->execute(array(':userID' => $userID)); 
$data = $checkInfo->fetch(PDO::FETCH_ASSOC);  
$count_rows = $checkInfo->rowCount(); 

//Here i am checking if data exists in mysql and UPDATING it  
if($count_rows > 0) { 

    if(!empty($userDesc)) { 
     $query = $con->prepare("UPDATE user_info SET description=:description WHERE userid=:userID"); 
     $query->execute(array(':userID' => $userID, ':description'=> $userDesc)); 
    } 
    if(!empty($userLocation)) { 
     $query = $con->prepare("UPDATE user_info SET location=:location WHERE userid=:userID"); 
     $query->execute(array(':userID' => $userID, ':location'=> $userLocation)); 
    } 
    if(!empty($userBirthDate)) { 
     $query = $con->prepare("UPDATE user_info SET birthDate=:birthDate WHERE userid=:userID"); 
     $query->execute(array(':userID' => $userID, ':birthDate'=> $userBirthDate)); 
    } 
    if(!empty($user_picture_name)) { 
     $query = $con->prepare("UPDATE user_info SET profile_pic=:profile_pic WHERE userid=:userID"); 
     move_uploaded_file($user_picture_tmp, 'user_pic/'.$user_picture_name); 
     $query->execute(array(':userID' => $userID, ':profile_pic'=> $user_picture_name)); 

    } 
    //But if data doesn't exists i want to INSERT IT and here is error 
} else { 

    if($userDesc AND $userLocation AND $userBirthDate AND $user_picture_name != '') { 

     $query = $con->prepare("INSERT INTO user_info(userid) VALUES(:userID)"); 
     $query->execute(array(':userID' => $userID)); 

     if(!empty($userDesc)) { 
      $query = $con->prepare("INSERT INTO user_info(description) VALUES(?) WHERE userid=?"); 
      $query->execute(array($userDesc, $userID)); 
     } 
     if(!empty($userLocation)) { 
      $query = $con->prepare("INSERT INTO user_info(location) VALUES(?) WHERE userid=?"); 
      $query->execute(array($userLocation, $userID)); 
     } 
     if(!empty($userBirthDate)) { 
      $query = $con->prepare("INSERT INTO user_info(birthDate) VALUES(?) WHERE userid=?"); 
      $query->execute(array($userBirthDate, $userID)); 
     } 
     if(!empty($user_picture_name)) { 
      $query = $con->prepare("INSERT INTO user_info(profile_pic) VALUES(?) WHERE userid=?"); 
      move_uploaded_file($user_picture_tmp, 'user_pic/'.$user_picture_name); 
      $query->execute(array($user_picture_name, $userID)); 

     } 
    } else {echo 'All fields are empty';} 

}      
} 

所以我的問題是爲什麼我收到這個錯誤,也是我這樣做是錯誤的方式還是有一些其他更好的方法來做到這一點。 這只是爲了學習的目的。

+0

沒有「插入......在那裏......」語法。你必須使用「更新...」 – Jens

+0

你是對的。但它仍然不起作用。 –

回答

0

你正在做很多插入!你應該只做一個插入!

嘗試用以下替換您的其他語句:

else { 
    if($userDesc AND $userLocation AND $userBirthDate AND $user_picture_name != '') { 

     if($userDesc = ''){$userDesc = 'Placeholder description';} 
     if($userLocation = ''){$userLocation = 'Placeholder location';} 
     if($userBirthDate = ''){$userBirthDate = 'Placeholder birthdate';} 
     if($user_picture_name = ''){$user_picture_name = 'Placeholder picture_name';} 

     if ($stmt = $con->prepare("INSERT INTO user_info(description, location, birthDate, profile_pic) VALUES(?,?,?,?)")) { 
      $stmt->bind_param("ssss", $userDesc, $userLocation,$userBirthDate, $user_picture_name); 
      $stmt->execute(); 
     } 
    } else {echo 'All fields are empty';} 
} 
+0

是的,但重點是如果輸入是空的我不想輸入,而是我想保留佔位符數據。 –

+0

好吧,我更新了代碼,以便您可以添加佔位符數據,如果這些字段爲空。 – Fabian