我有一个页面显示用户当前的个人信息和一个处理程序,它们遍历表单元素,将它们过滤到相关的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