Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO,检查空字符串和don';如果为空,则不更新_Php_Mysql_Pdo - Fatal编程技术网

Php PDO,检查空字符串和don';如果为空,则不更新

Php PDO,检查空字符串和don';如果为空,则不更新,php,mysql,pdo,Php,Mysql,Pdo,如果在没有任何参数的情况下运行函数edit_profile(),则空字符串将写入DB。例如,如果$input['email']为空,我希望UPDATE不更新此列 我试着做: SET email = IF(LENGTH(:email)=0, email, :email), 它不起作用,我不知道如何与上面的PDO相同 function edit_profile($input) { // $user_id = 1; // try { // $

如果在没有任何参数的情况下运行函数
edit_profile()
,则空字符串将写入DB。例如,如果
$input['email']
为空,我希望
UPDATE
不更新此列

我试着做:

SET email = IF(LENGTH(:email)=0, email, :email),
它不起作用,我不知道如何与上面的PDO相同

function edit_profile($input) {
    //
    $user_id = 1;
    //
    try {
    //

        $conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare('UPDATE users SET
                                    email = :email,
                                    password = :password,
                                    name_surname = :name_surname,
                                    age = :age,
                                    sex = :sex,
                                    education = :education,
                                    avatar = :avatar
                                    WHERE id = :id');


                $stmt->execute(array(
                    ':id'           => $user_id,
                    ':email'        => $input['email'],
                    ':password'     => $input['password'],
                    ':name_surname' => $input['name_surname'],
                    ':age'          => $input['age'],
                    ':sex'          => $input['sex'],
                    ':education'    => $input['education'],
                    ':avatar'       => $input['avatar']
                ));


        echo $stmt->rowCount(); // 1
    //
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    //
}
edit_profile();
试一试

我基本上是在尝试使用COALESCE()来使用
:email
,只要不为null。如果为空,请使用旧的
电子邮件


如果
:email
是空字符串,而不仅仅是
NULL
,我添加了
NULLIF
,将空字符串转换为
NULL
;)

@DontVoteMeDown他想更新而不是更新这个column@DontVoteMeDown你真的不明白吗?:)令人惊叹的!我喜欢coalesce:)@Marek,可能你是对的-没有检查。我用可能的解决方法更新了我的答案。你怎么认为?
UPDATE users SET
email = COALESCE(NULLIF(:email, ''),email),
password = :password,
name_surname = :name_surname,
age = :age,
sex = :sex,
education = :education,
avatar = :avatar
WHERE id = :id
SET email = IF(LENGTH(:email1)=0, email, :email2)

$stmt->execute(array(
                ':id'           => $user_id,
                ':email1'        => $input['email'],
                ':email2'        => $input['email'],
                ':password'     => $input['password'],
                ':name_surname' => $input['name_surname'],
                ':age'          => $input['age'],
                ':sex'          => $input['sex'],
                ':education'    => $input['education'],
                ':avatar'       => $input['avatar']
));