仅当value不为null时,PHP/MYSQL UPDATE [英] PHP/MYSQL UPDATE only if value is not null

查看:81
本文介绍了仅当value不为null时,PHP/MYSQL UPDATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个表单来更新多列数据.就我的目的而言,这段代码已部分成功.剩下要做的就是为更新查询添加(如果IS NOT NULL).我不确定该怎么做.

I'm building a form to update multiple columns of data. This code has been partially successful for my purposes. The only thing left to do is include (if IS NOT NULL) for the update query. I'm not sure how to go about this.

换句话说,我只希望$ _POST值不为空时执行UPDATE.

In other words, I only want the UPDATE to execute if the $_POST value is not empty.

<form>
<input type='text' name='input1' />
<input type='text' name='input2' />
<input type='text' name='input3' />
<input type='submit' value='submit' />
</form>

<?php
//db connect

$1=$_POST['input1'];
$2=$_POST['input2'];
$3=$_POST['input3'];

mysql_query("UPDATE table
             SET a = $1
                 b = $2
                 c = $3
             WHERE row = 'row_id");

);


?>

预先感谢您的帮助.

(为了使您免于额外的输入,我的原始代码转义了字符,因此不需要SQL注入警告.我也正在熟悉"mysqli_query",因此也无需对此发表评论)

(To save you from some extra typing, my original code escapes characters so warnings of SQL injections aren't necessary. I'm also in the process of familiarizing myself with "mysqli_query", so no need to comment on that either.)

推荐答案

您可以使用COALESCE

       UPDATE table
         SET a = COALESCE($1, a),
             b = COALESCE($2, b),
             c = COALESCE($3, c)
         WHERE row = ''

这篇关于仅当value不为null时,PHP/MYSQL UPDATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆