我有一個頁面顯示用戶當前的個人信息和一個處理程序,它們遍歷表單元素,將它們過濾到相關的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);
你可以更具體一點嗎?什麼是實際問題,什麼是phperror.log等......很難幫助你沒有起點。 – KB22 2009-09-03 14:47:44
這是什麼「不起作用」?如果腳本死了,嘗試添加'error_reporting(E_ALL); ini_set('display_errors',1);'到文件的頂部以查看任何錯誤消息。 – 2009-09-03 14:48:45
對不起,我猜我是模糊的。通常會發生的情況是,表單提交和url更改爲account_management.php5?res = suc,如果這些更改已成功完成,它將顯示該內容,但這些字段未更新以反映任何更改,我的數據庫表也不會更新。所以,它似乎遵循我的代碼沒有運行時錯誤,但沒有實際發生。 – user97410 2009-09-03 14:51:37