【问题标题】:update profile php更新配置文件 php
【发布时间】:2024-01-20 16:49:01
【问题描述】:

我已经在 php.ini 中创建了个人资料页面。该页面包括地址和电话字段,并提示用户插入他们的数据。然后将数据保存在我的名为 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 方法表现如何?

标签: php sql sql-update insert-into


【解决方案1】:

我是 psu 发布的答案的新手,我肯定会对此进行检查,但通过快速阅读,您在使用这些特殊语法时需要非常小心。

想到的 1 个原因:您不知道要插入或更新信息的表可能会发生什么。如果定义了多个唯一性,那么您可能会遇到严重的麻烦,这在扩展应用程序时很常见。

2 语法替换是我很少希望在我的应用程序中发生的功能。因为我不想从表中已经存在的行中丢失数据。

我并不是说他的回答是错误的,只是说明使用它时需要预防,因为上述原因可能还有更多。

如第一篇文章所述,我可能是这样做的新手,但此时我更喜欢:

$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();
    }
}

【讨论】:

  • 我已经使用上面的代码更改了函数。问题是表可以再次更新,但表为空时没有数据插入。我之前忘了提到我使用了 2 张桌子。第一个表称为 users 并包含 user_id。另一个名为 profile 的表包括(profile_id、address、telephone、user_id)。在配置文件表中,user_id 是外键。对于外键,我做了以下设置:内部关系:users.user_id 外键约束@​​987654322@.user_id ON DELETE RESTRICT ON UPDATE RESTRICT.
  • 为什么要在数据库中插入一个完全空的行?这与没有行一样吗?我想我没有注意到你问题中的那部分:p,我会更新答案,但我真的不明白为什么要这样做
  • 好的,我会解释的。我的数据库中有 2 个表。表 USERS 包含用户注册期间需要的 user_id 和其他一些信息。表 PROFILE 保存有关用户个人资料的信息。 user_id 是配置文件表的外键。所以我制作了一个表单,提示用户创建他的个人资料。此表单将收集有关用户配置文件的所有信息,并将它们存储在数据库的表配置文件中。如果该用户尚未创建他的个人资料,则个人资料表将为空(因此...插入)。如果用户已经创建了他的个人资料,他将能够使用相同的表单来更新他的个人资料(所以...更新)。
  • 是的,我想了这么多,但为什么要在数据库中插入一条空记录呢?如果用户提交了一个空表单,那是不值得提交到数据库的:如果数据是空的,你不应该在配置文件表中有一行,这并不是因为你有 FK,所以你总是需要设置关系,仅当关系存在时。因此,如果用户有个人资料,您可以通过插入行来建立关系,如果用户没有填写表格,那么您就不会插入行。无论如何,如果我的回答对你有帮助,请你接受。谢谢
  • 我不确定我是否得到了您的要求,如果我的回复不符合要求,敬请见谅。但是,如果您只复制粘贴我的代码,那么将查询发送到数据库的实际行将丢失,您应该将 var_dump($sql) 替换为 mysql_query($sql)。 var_dump($sql) 只是为了向您展示上面的代码在做什么。如果此评论对您没有帮助,也许您应该发布另一个问题。您也可以尝试“mysql_query($sql) 或 echo mysql_error()”来查看查询是否有任何错误。请参阅答案中的更新
【解决方案2】:

如果表中没有与 user_id 对应的任何数据,则无法更新表,这意味着您必须有一行包含 user_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

【讨论】:

    【解决方案3】:

    @Stefanos

    您可以在 SQL 查询中使用“REPLACE INTO”命令代替“INSERT INTO”。

    例如

    假设你有插入查询

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

    现在您可以使用以下查询作为插入和更新的组合

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

    希望这会有所帮助!

    【讨论】: