更新个人资料 PHP

4

我在php中创建了一个个人资料页面。该页面包括地址和电话字段,并提示用户插入其数据。然后将数据保存在我的名为“profile”的表中。 一切都很正常,但问题是只有当表已经包含数据时才更新表格。如何修改它(可能是我在函数中的mysql查询语句),以便即使为空也会输入数据到表中。是否有类似于UPDATE OR INSERT INTO语法的东西可以使用? 谢谢

<?php


if ( isset($_GET['success']) === true && empty($_GET['success'])===true ){
echo'profile updated sucessfuly';
}else{

 if( empty($_POST) === false  &&  empty($errors) === true ){

    $update_data_profile = array(
                'address' => $_POST['address'],
                'telephone' => $_POST['telephone'],
                );


   update_user_profile($session_user_id, $update_data_profile);

   header('Location: profile_update.php?success');

   exit();


}else if ( empty($errors) === false ){
   echo output_errors($errors);
}

?>

然后通过使用以下函数
function update_user_profile($user_id, $update_data_profile){

$update = array();

array_walk($update_data_profile, 'array_sanitize');

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


     mysql_query(" UPDATE `profile` SET " . implode(',  ', $update) . " WHERE `user_id` = $user_id ") or die(mysql_error());


}

当输入为空时,您的array_sanitize方法会如何表现? - Redian
3个回答

1

我对psu发布的答案感到陌生,但肯定会仔细研究。从快速阅读中可以得出一个结论,使用特殊语法时需要非常小心。

首先,你不知道正在插入或更新信息的表可能会发生什么情况。如果定义了多个唯一值,那么可能会遇到严重问题,而在扩展应用程序时这是很常见的事情。

其次,replace into语法是我很少希望在我的应用程序中发生的功能。因为我不想失去已经在表中的行中列的数据。

我并不是说他的答案是错的,只是需要注意上述原因和可能存在的其他问题。

正如第一篇文章中所述,我可能是个新手,但此时此刻我更喜欢:

$result = mysql_query("select user_id from profile where user_id = $user_id limit 1");
if(mysql_num_rows($result) === 1){
    //do update like you did
}
else{
    /** 
     *  this next line is added after my comment, 
     *  you can now also leave the if(count()) part out, since the array will now alwayss
     *  hold data and the query won't get invalid because of an empty array
     **/
    $update_data_profile['user_id'] = $user_id;
    if(count($update_data_profile)){
        $columns = array();
        $values = array();
        foreach($update_data_profile as $field => $data){
            $columns[] = $field;
            $values[] = $data;
        }
        $sql = "insert into profile (" .  implode(",", $columns) .") values ('" . implode("','", $values) . "')" ;
        var_dump($sql); //remove this line, this was only to show what the code was doing
        /**update**/
        mysql_query($sql) or echo mysql_error();
    }
}

1
我已经使用上述代码更改了函数。问题是,表格再次可以更新,但当它为空时,没有数据插入表格中。我之前忘记提到我使用了2个表。第一个表称为用户(users),包括user_id。另一个表称为个人资料(profile),包括(profile_id, address, telephone, user_id)。在个人资料表中,user_id是外键。对于外键,我进行了以下设置:内部关系:users.user_id 外键约束users.user_id ON DELETE RESTRICT ON UPDATE RESTRICT。 - 33528
为什么要向数据库插入完全空的行?这与没有行是一样的吗?我猜我在你的问题中没有注意到这一部分 :p,我会更新答案,但我真的不明白为什么有人会这样做。 - Bodybag
1
我不太确定你的问题是什么,如果我的回答有偏差,请见谅。但是,如果你只是复制粘贴了我的代码,那么发送查询到数据库的实际行已经丢失了,你应该用mysql_query($sql)替换var_dump($sql)。var_dump($sql)只是为了向你展示上面的代码在做什么。如果这个评论没有帮助到你,也许你应该发另一个问题。你也可以尝试“mysql_query($sql)或echo mysql_error()”来查看查询是否有错误。请参见答案中的更新。 - Bodybag
亲爱的Bobybag,感谢您上次的回复。多亏了您,它现在可以正常工作了。唯一缺少的是在$update_data_profile数组中插入'user_id' => $session_user_id。再次感谢您...您真是个天才。 - 33528
最后一个问题是:我们使用的上述代码每次更新时都会更新表中的所有字段。如何修改它,使其仅更新特定字段。例如,如果用户在表单中只插入了电话号码,则仅更新表中的电话字段,并保留其他字段不变。 - 33528
显示剩余4条评论

0

如果表中没有与用户ID对应的数据,那么您无法更新该表,这意味着您必须有一行包含用户ID和null或其他值的数据。

a) 您可以尝试检查表中是否包含数据,如果没有,则插入数据,否则使用更新(不是理想的方法)

$result = mysql_query("UPDATE ...");       
if (mysql_affected_rows() == 0)    
    $result = mysql_query("INSERT ...");
b) 请查看以下链接

http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html

http://dev.mysql.com/doc/refman/5.0/en/replace.html

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html


0

@Stefanos

你可以在 SQL 查询中使用 "REPLACE INTO" 命令代替 "INSERT INTO"。

例如:

假设你有一个插入查询:

INSERT INTO EMPLOYEE (NAME,ADD) values ('ABC','XYZZ');

现在你可以使用以下查询作为插入和更新的组合。
REPLACE INTO EMPLOYEE (NAME,ADD) values ('ABC','XYZZ');

希望这能有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接