2016-01-20 48 views
0

我正在爲用戶編輯其數據提供一種方法。我的第一個方法是我工作,但後來我記得它非常不安全,我不應該直接將數據插入到數據庫中;至少這是我被告知的。我嘗試通過執行VALUES(?,?,?,?,?)來使它更安全,以便數據不會直接進入,這似乎在我的註冊頁面中正常工作(如果您想要, )。無法爲用戶更新數據創建安全的方式

開始,這裏是工作的罰款我原來的更新數據頁,但它不使用方法(,,,,?????):

if(isset($_POST['submit'])) { 
    $userid=$_SESSION['userid']; 
    $skype=$_POST['skype']; 
    $email=$_POST['email']; 
    $region=$_POST['region']; 
    $crank=$_POST['league1']; 
    $drank=$_POST['league2']; 
     if(empty($skype) || empty($email) || empty($crank) || empty($drank) || empty($region)) 
     { 
      echo "Cannot leave any field blank"; 
     }  
     else 
     { 
      $host= "localhost"; 
      $dbname = "boost"; 
      $user = "root"; 
      $pwd = ""; 
      $port=3306; 

      try 
      { 
       $mysqli= new mysqli($host, $user, $pwd, $dbname,$port); 
       if ($mysqli->connect_error) { 
       die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); 
      } 
      $query = "UPDATE usertable SET SkypeID = '$skype', Email = '$email', Region = '$region', CRank = '$crank', DRank = '$drank' WHERE UserID = '$userid'"; 
      $stmt = $mysqli->prepare($query); 
      $stmt->bind_param("sssss",$skype,$email,$region,$crank,$drank); 
      $stmt->execute(); 
      $iLastInsertId=$mysqli->insert_id; 
      header('Location: http://localhost/Boost/account.php'); 
      $stmt->close(); 
      $mysqli->close(); 
     } catch (mysqli_sql_exception $e) { 
     throw $e; 
    } 
    } 
} 

這裏是我試圖做使其更安全,但這似乎並不奏效。具體的$query = "UPDATE usertable SET usertable(SkypeID,Email,Region,CRank,DRank) VALUES (?,?,?,?,?) WHERE UserID = '$userid'";似乎是問題,雖然語法看起來好像沒什麼問題

if(isset($_POST['submit'])) { 
    $userid=$_SESSION['userid']; 
    $skype=$_POST['skype']; 
    $email=$_POST['email']; 
    $region=$_POST['region']; 
    $crank=$_POST['league1']; 
    $drank=$_POST['league2']; 
     if(empty($skype) || empty($email) || empty($crank) || empty($drank) || empty($region)) 
     { 
      echo "Cannot leave any field blank"; 
     }  
     else 
     { 
      $host= "localhost"; 
      $dbname = "boost"; 
      $user = "root"; 
      $pwd = ""; 
      $port=3306; 

      try 
      { 
       $mysqli= new mysqli($host, $user, $pwd, $dbname,$port); 
       if ($mysqli->connect_error) { 
       die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); 
      } 
      $query = "UPDATE usertable SET usertable(SkypeID,Email,Region,CRank,DRank) VALUES (?,?,?,?,?) WHERE UserID = '$userid'"; 
      $stmt = $mysqli->prepare($query); 
      $stmt->bind_param("sssss",$skype,$email,$region,$crank,$drank); 
      $stmt->execute(); 
      $iLastInsertId=$mysqli->insert_id; 
      header('Location: http://localhost/Boost/account.php'); 
      $stmt->close(); 
      $mysqli->close(); 
     } catch (mysqli_sql_exception $e) { 
     throw $e; 
    } 
    } 
} 

所以我不知道是什麼問題。在我使用PHP的經驗中,語法應該沒問題,但我必須缺少一些東西。

+0

你使用什麼數據庫? SQL更新值語法是不尋常的。你確定它有效嗎?爲什麼要改變以前的更傳統的語法? – Thilo

+4

試試'UPDATE usertable SET SkypeID =?,Email =?,Region =?,CRank =?,DRank =? WHERE UserID ='$ userid'' – Matt

+1

您是否收到任何錯誤訊息?通常這些有助於理解錯誤。 – Thilo

回答

2

這是很簡單實際上,你從

$query = "UPDATE usertable SET SkypeID = '$skype', Email = '$email', Region = '$region', CRank = '$crank', DRank = '$drank' WHERE UserID = '$userid'"; 

$query = "UPDATE usertable SET usertable(SkypeID,Email,Region,CRank,DRank) VALUES (?,?,?,?,?) WHERE UserID = '$userid'"; 

看來改寫所以解決您只需使用舊的語句時,你感到困惑的INSERT語句與UPDATE語句與新的風格...

$query = "UPDATE usertable SET SkypeID = ?, Email = ?, Region = ?, CRank = ?, DRank = ? WHERE UserID = $userid"; 
+0

哦,你的權利。我認爲他們會是一樣的。我改變了這一點。然而,當我改變了代碼,我得到了一個錯誤:未捕獲的異常'mysqli_sql_exception'消息'重複條目'?'關鍵'電子郵件'' – Voken

+0

Nevermind,@Matt的代碼工作。奇怪的是,我猜(')不應該在那裏?我甚至不知道 – Voken

+0

正確的沒有引號,我已經編輯了我的答案,以反映更新後的值(從我原來的腳本中哈希複製和粘貼作業,我很抱歉)。 – JasonSec

相關問題