简体   繁体   English

带有 if else 条件的 SQL UPDATE

[英]SQL UPDATE with if else conditions

I want to update the columns post_author and post_user with variables if the respective columns are not empty in the table.如果表中的相应列不为空,我想用变量更新列post_authorpost_user If to take off those particular conditions and leave only one, for example post_author , then everything is working, I have problems only with the sql 'case' part.如果post_author那些特定条件并只留下一个,例如post_author ,那么一切正常,我只有 sql 'case' 部分有问题。

Having this issue:遇到这个问题:

Notice: Fail You have an error in your SQL syntax;注意:失败 您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET post_author = case when post_author !=null then 'evgen' else null end, SET p' at line 1检查与您的 MariaDB 服务器版本相对应的手册,了解在 'SET post_author = case when post_author !=null then 'evgen' else null end, SET p' at line 1 附近使用的正确语法

$query = "UPDATE posts SET post_title = '{$post_title}', 
post_tags= '{$post_tags}', 
post_date = now(), 
post_image = '{$post_image}', 
post_content = '{$post_content}',  
post_status = '{$post_status}', 
post_category_id = '{$post_category_id}', 
SET post_author = case when post_author !=null then '{$post_author}' 
                                               else null end, 
SET post_user = case when post_user !=null then '{$post_user}' 
                                           else null end 
WHERE post_id = $the_great_post_id ";

I have this HTML:我有这个 HTML:

<?php  
if(null !=  $post_user) {
?>
      <div class="form-group">
        <label for="post_user">User</label>

       <input type="text" class="form-control" name="post_user" value="<?php echo $post_user; ?>">
    </div>

<?php   }
if(null !=  $post_author) {
?>
      <div class="form-group">
        <label for="post_author">Author</label>

       <input type="text" class="form-control" name="post_author" value="<?php echo $post_author; ?>">
       </div>

<?php   }  ?>

You don't need SET for the last two values最后两个值不需要SET

  $query = "
      UPDATE posts 
      SET post_title = '{$post_title}', 
      post_tags= '{$post_tags}', 
      post_date = now(), 
      post_image = '{$post_image}',
      post_content = '{$post_content}', 
      post_status = '{$post_status}', 
      post_category_id = '{$post_category_id}', 
      post_author = case when post_author !=null then '{$post_author}' else null end, 
      post_user = case when post_user !=null then '{$post_user}' else null end 
      WHERE post_id = $the_great_post_id ";

Anyway you should not use PHP variables in SQL, doing so puts you are at risk for SQL injection.无论如何,您不应该在 SQL 中使用 PHP 变量,这样做会使您面临 SQL 注入的风险。 To avoid this you should take a look at prepared statement and binding param for your PHP db driver.为避免这种情况,您应该查看 PHP 数据库驱动程序的准备好的语句和绑定参数。

When you have当你有

SET post_author = case when post_author !=null then '{$post_author}' 
                                               else null end,

there are a few problems,firstly you don't need the SET .有几个问题,首先你不需要SET Secondly, using else null will set the value to null rather than leaving the field with it's original value.其次,使用else null会将值设置为 null 而不是保留字段的原始值。

In this version, it uses...在这个版本中,它使用...

post_author = case when post_author is null then '{$post_author}' else post_author end, 

which put together gives you...放在一起给你...

UPDATE posts 
    SET post_title = '{$post_title}', 
        post_tags= '{$post_tags}', 
        post_date = now(), 
        post_image = '{$post_image}', 
        post_content = '{$post_content}', 
        post_status = '{$post_status}', 
        post_category_id = '{$post_category_id}', 
        post_author = case when post_author is null then '{$post_author}' else post_author end, 
        post_user = case when post_user is null then '{$post_user}' else post_user end 
    WHERE post_id = $the_great_post_id 

The other thing to point out is that you should be using prepared statements as this is insecure and can allow all sorts of problems.另一件事要指出的是,您应该使用准备好的语句,因为这是不安全的,并且可能会出现各种问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM