尝试更新

时间:2015-05-28 11:46:23

标签: php mysql pdo

我正在尝试更新用户信息,但工作正常,但

当我只更新单列时有5列其他四列值变为空(它覆盖以前的值)

<?php

require('includes/config.php');

$username = $_SESSION['username'];
$fullname = htmlspecialchars($_POST['fullname']);
$dob = htmlspecialchars($_POST['dob']);
$gend = htmlspecialchars($_POST['gend']);
$address = htmlspecialchars($_POST['address']);
$intrested = htmlspecialchars($_POST['intrested']);

try {
    $stmt = $db->prepare("UPDATE user SET fullname = :fullname, 
            dob = :dob, 
            gend = :gend,  
            address = :address,  
            intrested = :intrested  
            WHERE username = :username");
    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    $stmt->bindParam(':dob', $dob, PDO::PARAM_STR);
// use PARAM_STR although a number  
    $stmt->bindParam(':gend', $gend, PDO::PARAM_STR);
    $stmt->bindParam(':address', $address, PDO::PARAM_STR);
    $stmt->bindParam(':intrested', $intrested, PDO::PARAM_STR);
    $stmt->execute();
} catch (PDOException $e) {
    echo $e->getMessage();
}


header('Location: profile.php');
?>

这是我更新用户信息的代码

当我只更新一列时,其他四列值应相同

可以帮助我一些人

1 个答案:

答案 0 :(得分:0)

可能有类似的东西吗?

$sql = "UPDATE user SET ";
foreach($_POST as $k => $v) {
    if(empty($v)) continue;
    $sql .= "$k = :$k";

    $stmt->bindParam(':$k', $v, PDO::PARAM_STR);
}

$sql .= " WHERE username = :username";

当然需要做更多的事情(例如,检查它是否是PDO :: PARAM_STR)。

也是&#34;映射&#34;建议使用合适的色谱柱

<强>更新

(以下不会按原样运作 - 只是给你一些想法)

高级解决方案可能是使用* update方法扩展PDO类**

    public function update($table, $pairs = array(), $condition = "", $mode = null){
            if($mode == null)
                $mode = self::STMT_UPDATE;
            $sql = $this->buildStatement($table, $pairs, $mode);

            $params = array();
            $sql.= $condition;

            if($mode == self::STMT_UPDATE)
                $params = array_merge($pairs, $params);

            $stmt = $this->prepare($sql);
            return $stmt->execute($params);
    }

    /**
     * INTERNAL method to build up all statements
     * 
     * @param string $table
     * @param array $pairs
     * @param number $statementType
     * @throws Exception
     * @return string
     */
    private function buildStatement($table, $pairs = array(), $statementType = 0){
            if(!is_array($pairs) && ($statementType & ~9)) throw new \Exception("buildStatement failed: parameter \$pairs is invalid");
            if(empty($pairs) && ($statementType & ~9)) throw new \Exception("buildStatement failed: parameter \$pairs is empty");

            $sql = "";
            switch ($statementType) {
                    default:
                    case self::STMT_SELECT:
                            $sql.= "SELECT ";
                            if(count($pairs) > 0)
                                    $sql.= implode(', ', $pairs);
                            else
                                    $sql.= '*';

                            $sql.= ' FROM '.$table;
                            break;
                    case self::STMT_INSERT:
                            $tableFields = array_keys($pairs);
                            $sql.= 'INSERT INTO ' . $table . ' (`' . implode('`, `', $tableFields) . '`) VALUES (:' . implode(', :', $tableFields) . ')';

                            break;
                    case self::STMT_UPDATE:
                            $setQuery = array();
                            foreach ($pairs as $field => &$value){
                                $setQuery[] = '`' . $field . '` = :' . $field;
                            }
                            $sql.= 'UPDATE ' . $table . ' SET ' . implode(', ', $setQuery);
                            break;
                    case self::STMT_UPDATEADV:
                            foreach ($pairs as $field => &$value){
                                $setQuery[] = '`' . $field . '` = ' . $value;
                            }
                            $sql.= 'UPDATE ' . $table . ' SET ' . implode(', ', $setQuery);
                            break;
                    case self::STMT_DELETE:
                            $sql.= 'DELETE FROM ' . $table;
                            break;
            }

            return $sql;
    }