如何防止MySQL用空字符串更新列?

编程入门 行业动态 更新时间:2024-10-26 16:25:08
本文介绍了如何防止MySQL用空字符串更新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个表格来更新用户的详细信息,他们不必填写每个字段.因此,它将通过POST向数据库传递一个空字符串.这是我的查询:

I have a form to update details of a user and they do not have to fill in every field. Because of that, it will be passing an empty string to the database via POST. This is my query:

$q = "UPDATE mdl_user SET firstname='$fname', lastname='$lname', email='$email', address='$address', city='$city', school='$school', phone1='$phone' WHERE id='$uid'";

MySQL是否可以检查例如$fname是否为空字符串,如果为空,则不更新该字段?

Is there a way for MySQL to check if for example $fname is an empty string and if it's empty don't update that field?

如果没有,那我应该如何用PHP做到这一点?

If not then how should I go about doing this in PHP?

推荐答案

您可以对需要检查的每个字段使用IF语句.例如:

You can use IF statement for each field you need to check. For example :

$q = "UPDATE mdl_user SET firstname=IF(LENGTH('$fname')=0, firstname, '$fname'), lastname=IF(LENGTH('$lname')=0, lastname, '$lname'), email=IF(LENGTH('$email')=0, email, '$email'), address='$address', city='$city', school='$school', phone1='$phone' WHERE id='$uid'";

更多推荐

如何防止MySQL用空字符串更新列?

本文发布于:2023-10-08 13:55:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何防止   空字符串   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!