2009-09-03 59 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