0

我正在创建一个用户设置页面,允许成员更新他们的信息。信息显示,但是一旦更改不会在数据库上更新,尽管没有产生错误并显示成功页面。我试过回显数组,它似乎是空白的,这意味着它没有解析任何新数据。

<?php
include ("storescripts/init.php");
protect_page();
if (empty($_POST) === false) {
$required_fields = array('mem_email','mem_first_name','mem_last_name');
foreach ($_POST as $key=>$value) {
    if (empty($value) && in_array($key, $required_fields) == true) {
        $errors[] = 'Fields marked with an asterisk are required';
        break 1;
    }
}
if (empty($errors) === true) {
    if (filter_var($_POST['mem_email'], FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = 'A valid email address is required';
    } else if(user_exists($_POST['mem_email']) === 1 && $member_data['mem_email'] !== $_POST['mem_email']) {
        $errors[] = 'Sorry, the email \'' . htmlentities($_POST['mem_email']) . '\' is already in use';
    }
}
}
if (empty($_POST) === false && empty($errors) === true) {

$update_data = array(
        'mem_first_name' => $_POST['mem_first_name'],
        'mem_last_name' => $_POST['mem_last_name'],
        'mem_email' => $_POST['mem_email'],
        'allow_email' => ($_POST['allow_email']) ? 1 : 0);

        update_user($session_member_id, $update_data);
        header('Location: settings.php?success=1');
        die();
}
include ("includes/overall/head.php");
?>
<?php include ("includes/overall/template_header.php");?>
<div id="mainDivShort">
    <h1>User Settings</h1>
    <div id="divBreak"></div>
    <?php include ("includes/overall/column_left.php");?>
    <div id="middleContent">
    <?php if (isset($_GET['success']) && isset($_GET['success'])) {
            echo 'Your details have been updated';
            } else {
                if (empty($errors) === false) {
                echo output_errors($errors);
                }?>
        <form action="" method="post">
            <ul>
                <li>First Name*: <br> <input type="text"    name="mem_first_name"
                    value="<?php echo  $member_data['mem_first_name'];?>">
                </li>
                <li>Last Name*: <br> <input type="text"  name="mem_last_name"
                    value="<?php echo  $member_data['mem_last_name'];?>">
                </li>
                <li>Email*: <br> <input type="text" name="mem_email"
                    value="<?php echo $member_data['mem_email'];? >">
                </li>
                <li>
                    <input type="checkbox" name="allow_email" <? php if ($member_data['allow_email'] == 1) { echo 'checked="checked"'; } ?>> Would you like to  receive an email about news and promotions?
                </li>
                <li><input type="submit" value="Update">
                </li>
            </ul>
        </form>
        <?php }?>
    </div>
    <?php include ("includes/overall/column_right.php");?>
</div>
<?php include ("includes/overall/template_footer.php");?>

还有我的 update_user 函数

function update_user($mem_id, $update_data) {
$update = array();
array_walk($update_data, 'array_sanitize');

foreach ($update_data as $field=>$data) {
    $update[] = '`' . $field . '` = \'' . $data . '\'';
}

mysql_query("UPDATE `members` SET " . implode(', ', $update) . " WHERE `mem_id`= $mem_id");
}
4

1 回答 1

0

您是否在 update_user 函数中收到正确的数据?

尝试这个

function update_user($mem_id, $update_data) {
    $update = array();
    array_walk($update_data, 'array_sanitize');

    foreach ($update_data as $field=>$data) {
        $update[] = '`' . $field . '` = \'' . $data . '\'';
    }

    $sql = "UPDATE `members` SET " . implode(', ', $update) . " WHERE `mem_id`= $mem_id";
   echo $sql; exit(); 

    //mysql_query("UPDATE `members` SET " . implode(', ', $update) . " WHERE `mem_id`= $mem_id");
    }

在您的数据库中触发此 $sql 输出以检查您是否有 sql 错误或任何其他数据库错误

于 2013-03-02T00:06:01.537 回答