如何在更新 MySQL 记录时不包含空白字段
如何在 MySQL 更新查询中不包含留空表单的文本字段?我理解为什么它用空字符串替换填充字段,但我不确定是否有有效的方法来修复它。最好的选择真的只是很多 if
语句吗?我可以使用某种函数来禁止 html 表单中的空白字段吗?
这是我到目前为止所得到的:
//Check if record exists
if(mysql_num_rows(mysql_query("SELECT Item_Id FROM Item_t WHERE Item_Id = '$itemid'")) == 0){
die('The Item ID you entered was not found. Please go back and try again.');
}
//Update record
$update = "UPDATE Item_t SET Item_Name='$itemname', Item_Price='$itemprice' WHERE Item_Id='$itemid'";
mysql_query($update);
所以基本上,在这个例子中,如果您将设置 $itemname
的字段留空并只更新 $itemprice
,价格就会更新,但名称将被设置为空字符串。
How would I not include text fields of a form left blank in a MySQL update query? I understand why it's replacing filled fields with empty strings, but I'm not sure of an efficient way to fix it. Is the best option really just a lot of if
statements? Is there some kind of function I could use to disallow blank fields in my html form?
Here's what I have so far:
//Check if record exists
if(mysql_num_rows(mysql_query("SELECT Item_Id FROM Item_t WHERE Item_Id = '$itemid'")) == 0){
die('The Item ID you entered was not found. Please go back and try again.');
}
//Update record
$update = "UPDATE Item_t SET Item_Name='$itemname', Item_Price='$itemprice' WHERE Item_Id='$itemid'";
mysql_query($update);
So basically, in this example, if you leave the field that sets $itemname
blank and just update $itemprice
, the price will be updated, but the name will be set to an empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查 SQL 中的字符串是否为空:
You can check if your strings are empty in the SQL:
如果您在 php 中构建更新字符串,如果它为空,您也可以以不同的方式构建它:
If you're building the update string in php, you might as well just build it differently if it's empty:
尝试这样的事情。
try something like this.