Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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 使用if-else条件进行SQL更新_Php_Mysql - Fatal编程技术网

Php 使用if-else条件进行SQL更新

Php 使用if-else条件进行SQL更新,php,mysql,Php,Mysql,如果表中相应的列不是空的,我想用变量更新列post\u author和post\u user。如果去掉那些特殊条件,只留下一个,例如post_author,那么一切都正常,我只有sql“case”部分有问题 关于这个问题: 注意:如果您的SQL语法有错误,则失败;检查手册 对应于正确语法的MariaDB服务器版本 使用near'设置post_author=post_author时的大小写=那就空了 “evgen”else null end,在第1行设置p 我有这个HTML: <?php

如果表中相应的列不是空的,我想用变量更新列
post\u author
post\u user
。如果去掉那些特殊条件,只留下一个,例如
post_author
,那么一切都正常,我只有sql“case”部分有问题

关于这个问题:

注意:如果您的SQL语法有错误,则失败;检查手册 对应于正确语法的MariaDB服务器版本 使用near'设置post_author=post_author时的大小写=那就空了 “evgen”else null end,在第1行设置p

我有这个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   }  ?>


使用者

您不需要为最后两个值设置

  $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 ";
无论如何,您不应该在SQL中使用PHP变量,这样做会使您面临SQL注入的风险。为了避免这种情况,您应该查看PHP db驱动程序的准备语句和绑定参数

SET post_author = case when post_author !=null then '{$post_author}' 
                                               else null end,
有几个问题,首先您不需要
集合
。其次,使用
else null
将值设置为null,而不是保留字段的原始值

在这个版本中,它使用

post_author = case when post_author is null then '{$post_author}' else post_author end, 
把它们放在一起会让你

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 

另一点需要指出的是,您应该使用准备好的语句,因为这是不安全的,可能会出现各种各样的问题。

然后我在上面添加了if(null!=$$$u POST['POST\u author']){$POST\u author=$$$u POST['POST\u author'];}elseif(null!=$$u POST\u user=$$POST['POST\u user']}else){null=$\u POST['POST\u author'];null=$\u POST['POST\u user'];}但无论如何,作者或用户都没有被更新和删除,而您的问题是关于sql错误的。我的答案是(希望您没有更多的sql错误)…我不在您的代码上下文中。实际上,这也必须添加到代码中,而不是sql if(isset)中($\u POST['POST\u author']){$POST\u author=$\u POST['POST\u author'];}如果(isset($\u POST['POST\u user']){$POST\u user=$\u POST['POST\u user'];}现在一切正常!
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