2013-12-18 86 views
4

有誰知道爲什麼PHP代碼沒有更新列pictures將更新他們的休息,但只是沒有圖片列是更新用戶信息,以便電子郵件地址,密碼和圖片我是很新的PHP,所以我真的不知道要尋找什麼,當即時尋找錯誤爲什麼心不是我的PHP代碼更新SQL

<?php 

    require("common.php"); 

    if(empty($_SESSION['user'])) 
    { 
     header("Location: login.php"); 

     die("Redirecting to login.php"); 
    } 

    if(!empty($_POST)) 
    { 
     if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
     { 
      die("Invalid E-Mail Address"); 
     } 

     if($_POST['email'] != $_SESSION['user']['email']['picture']) 
     { 
      $query = " 
       SELECT 
        1 
       FROM users 
       WHERE 
        email = :email 
        picture = :picture 
      "; 

      $query_params = array( 
       ':email' => $_POST['email'] 
      ); 

      try 
      { 
       $stmt = $db->prepare($query); 
       $result = $stmt->execute($query_params); 
      } 
      catch(PDOException $ex) 
      { 
       die("Failed to run query: " . $ex->getMessage()); 
      } 

      $row = $stmt->fetch(); 
      if($row) 
      { 
       die("This E-Mail address is already in use"); 
      } 
     } 

     if(!empty($_POST['password'])) 
     { 
      $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
      $password = hash('sha256', $_POST['password'] . $salt); 
      for($round = 0; $round < 65536; $round++) 
      { 
       $password = hash('sha256', $password . $salt); 
      } 
     } 
     else 
     { 
      $password = null; 
      $salt = null; 
     } 

     $query_params = array( 
      ':email' => $_POST['email'], 
      ':user_id' => $_SESSION['user']['id'], 
      ':picture' => $_POST['picture'], 
     ); 

     if($password !== null) 
     { 
      $query_params[':password'] = $password; 
      $query_params[':salt'] = $salt; 
     } 

     $query = " 
      UPDATE users 
      SET 
       email = :email 
       picture = :picture 
     "; 

     if($password !== null) 
     { 
      $query .= " 
       , password = :password 
       , salt = :salt 
      "; 
     } 

     $query .= " 
      WHERE 
       id = :user_id 
     "; 

     try 
     { 
      // Execute the query 
      $stmt = $db->prepare($query); 
      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex) 
     { 
      die("Failed to run query: " . $ex->getMessage()); 
     } 

     $_SESSION['user']['email']['picture'] = $_POST['email']; 

     header("Location: private.php"); 

     die("Redirecting to private.php"); 
    } 

?> 
+4

+1的實際使用準備好的語句和驗證輸入 –

+0

您需要添加'電子郵件=之間的逗號:電子郵件圖片=:picture'。你正在檢查'$ _POST ['picture']'是否被設置? – andrewsi

+1

還需要在SELECT的WHERE子句中使用AND,否? –

回答

2

你失蹤後一個逗號:電子郵件在您的UPDATE語句。

$query = " 
      UPDATE users 
      SET 
       email = :email 
       picture = :picture 
     "; 

應該

$query = " 
     UPDATE users 
     SET 
      email = :email, 
      picture = :picture 
    "; 

編輯:除了這個,你也缺少在第一個查詢參數:

 $query = " 
      SELECT 
       1 
      FROM users 
      WHERE 
       email = :email 
       picture = :picture 
     "; 

     $query_params = array( 
      ':email' => $_POST['email'] 
     ); 

通知你如何只應用:電子郵件你查詢參數,但您的查詢需要:email和:圖片。

你要麼需要從$query刪除picture = :picture或添加':picture' => $_POST['picture']$query_params

+0

好吧,我剛剛更新它,它還是老樣子dosnt似乎工作 – user3116240

+0

檢查以確保你的表單輸入有'NAME =「圖片」',拼寫正確。你確定'picture'不是一個文件嗎? – Leng

+1

在$ query語句之前嘗試var_dump($ _ POST ['picture'])並查看該值是什麼。 – Matt

3

你缺少一個,

$query = " 
    UPDATE users 
    SET 
     email = :email 
     picture = :picture 
"; 

您需要將其更改爲

$query = " 
    UPDATE users 
    SET 
     email = :email, 
     picture = :picture 
"; 
相關問題