2009-09-03 48 views
0

我有一個頁面顯示用戶當前的個人信息和一個處理程序,它們遍歷表單元素,將它們過濾到相關的mysql查詢中。有兩個表格,一個表格包含主數據,例如用戶名,電子郵件,密碼哈希,還有一個地址數據。然而,腳本不起作用,我不明白爲什麼。我已經完成了很多。恐怕這段時間很長,但理解邏輯完全相關。這裏是...更新個人信息PHP腳本

if(!$_POST) { 
    //come directly via address bar 
    header("Location: index.hmtl"); 
    exit; 
} 
//loop through all the post variables 

foreach ($_POST as $k => $v) { 

    if(eregi("confirm",$k) || eregi("old",$k)) { 
//the field in question is a duplicate one or there for authentication purposes and shouldn't be added to a table 
    continue; 
    } 

    if($k == "address" || $k == "town" || $k == "city" || $k == "postcode") { 

    //use aromaAddress table 


     $v = trim(htmlspecialchars(check_chars_mailto(mysqli_real_escape_string($mysqli,$v)))); 

     if(empty($v)) { 
//the field is empty...do nothing 
      continue; 
     } 

    //create query 
    $update_sql = "UPDATE aromaAddress SET ".$k." = '".$v."' WHERE userid = '".$_SESSION["userid"]."'"; 
    $update_res = mysqli_query($mysqli, $update_sql) or die(mysqli_error($mysqli)); 

    //add to session for the sake of having the form fields filled in next time 

    $_SESSION["$k"] = $v; 
    session_write_close(); 



    } else { 
    //sanitize them 

    $v = trim(htmlspecialchars(mysqli_real_escape_string($mysqli,check_chars_mailto($v)))); 

      if(empty($v)) { 
      continue; 
     } 

    if(eregi("email",$k)) { 

    if($_POST["email"] != $_POST["confirmEmail"]) { 
     header("Location: account_management.php5?error=ef"); 
     exit(); 
    } 

    $_SESSION["$k"] = $v; 
     session_write_close(); 

    //if email address/username being changed, check for pre-existing account with new address/username 

    $check_sql = "SELECT id FROM aromaMaster WHERE email='".$v."'"; 
    $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli)); 

    if(mysqli_num_rows($check_res) >= 1) { 
    //duplicate entry 
    mysqli_free_result($check_res); 
    header("Location: account_management.php5?error=email"); 
    exit; 
    } 
    } else if(eregi("username",$k)) { 

     if($_POST["username"] != $_POST["confirmUsername"]) { 
     header("Location: account_management.php5?error=ef"); 
     exit(); 
    } 


    $v = trim(htmlspecialchars(mysqli_real_escape_string($mysqli,check_chars_mailto($v)))); 

    //check for pre-existing account with same username 
     $check_sql = "SELECT id FROM aromaMaster WHERE username='".$v."'"; 
    $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli)); 

    if(mysqli_num_rows($check_res) >=1) { 
    //duplicate entry 
    mysqli_free_result($check_res); 
    header("Location: account_management.php5?error=username"); 
    exit; 
    } 

    } else if(eregi("newPassword",$k)) { 

     if(($_POST["newPassword"] != $_POST["confirmNewUsername"]) || ($_POST["oldPassword"] != $_POST["confirmOldPassword"])) { 
     header("Location: account_management.php5?error=ef"); 
     exit(); 
    } 


    $v = trim(htmlspecialchars(mysqli_real_escape_string($mysqli,check_chars_mailto($v)))); 

    //check for pre-existing account with same username 
     $check_sql = "SELECT id FROM aromaMaster WHERE id='".$_SESSION["userid"]."'"; 
    $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli)); 

    if(mysqli_num_rows($check_res) >=1) { 
    //duplicate entry 
    mysqli_free_result($check_res); 
    header("Location: account_management.php5?error=username"); 
    exit; 
    } 
} else { 

     $v = trim(htmlspecialchars(check_chars_mailto(mysqli_real_escape_string($mysqli,$v)))); 

    //create query 
    $update_sql = "UPDATE aromaMaster SET ".$k." = '".$v."' WHERE id = '".$_SESSION["userid"]."'"; 
    $update_res = mysqli_query($mysqli, $update_sql) or die(mysqli_error($mysqli)); 

$_SESSION["$k"] = $v; 
     session_write_close(); 
     header("Location: account_management.php5?res=suc"); 
     exit(); 
} 
    } 
    } 
    mysqli_close($mysqli); 
+0

你可以更具體一點嗎?什麼是實際問題,什麼是phperror.log等......很難幫助你沒有起點。 – KB22 2009-09-03 14:47:44

+0

這是什麼「不起作用」?如果腳本死了,嘗試添加'error_reporting(E_ALL); ini_set('display_errors',1);'到文件的頂部以查看任何錯誤消息。 – 2009-09-03 14:48:45

+0

對不起,我猜我是模糊的。通常會發生的情況是,表單提交和url更改爲account_management.php5?res = suc,如果這些更改已成功完成,它將顯示該內容,但這些字段未更新以反映任何更改,我的數據庫表也不會更新。所以,它似乎遵循我的代碼沒有運行時錯誤,但沒有實際發生。 – user97410 2009-09-03 14:51:37

回答

2

什麼是不工作?這很難猜測......

你不應該使用erigi檢查一個子字符串:1)它已被棄用2)使用stripos來代替。

編輯:

代碼尖叫SQL注入!

+0

看到上面的症狀...基本上,沒有任何反應!儘管如此,我會換掉eregi來換取stripos,謝謝。 – user97410 2009-09-03 14:59:48

+0

沒有任何症狀,我唯一能看到的就是你的代碼有一個註釋「它不工作的方式它應該«... – knittl 2009-09-03 15:17:35

+0

好的,發現'em xD在標題後('Location:...')你的$ _POST數組將是空的,這可能是一個問題嗎? – knittl 2009-09-03 15:19:57

0

提交了哪些數據(即$_POST中的內容)?

您的foreach($_POST as $k => $v)循環封裝在整個代碼塊中,所以如果您提交除用戶名和電子郵件地址之外的任何內容,則無法保證在重定向到res=suc之前將更新數據庫URL。

其他人提到了SQL注入的可能性。它看起來像是你正在逃避$v,但你沒有做任何事情來防止人們在$k餡狗屎。

最後,您的res=suc是默認選項。即您的成功標準和重定向發生在代碼中未明確編碼和處理的任何$k值。

+0

Hiya。我幾乎決定以另一種方式來做這件事 - 實際上是複製我的註冊腳本,並要求整個表格完整,並且只是更新所有內容,而不是通過字段的方式。要考慮的因素的數量使得它耗費更多時間。至於人們用$ k來填充狗屎,他們可以,但是我的表中沒有專欄來匹配它,所以不會插入任何內容,腳本會在插入時破壞。 – user97410 2009-09-03 20:02:00