如何在更新 MySQL 记录时不包含空白字段

发布于 2024-12-19 07:01:38 字数 633 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-12-26 07:01:38

您可以检查 SQL 中的字符串是否为空:

UPDATE Item_t
SET Item_Name = IF('$itemname' = '', Item_Name, '$itemname'),
    Item_Price = IF('$itemprice' = '', Item_Price, 'itemprice')
WHERE Item_Id='$itemid'

You can check if your strings are empty in the SQL:

UPDATE Item_t
SET Item_Name = IF('$itemname' = '', Item_Name, '$itemname'),
    Item_Price = IF('$itemprice' = '', Item_Price, 'itemprice')
WHERE Item_Id='$itemid'
小ぇ时光︴ 2024-12-26 07:01:38

如果您在 php 中构建更新字符串,如果它为空,您也可以以不同的方式构建它:

$update = "UPDATE Item_t SET ".($itemname ? "Item_Name='$itemname', " : "")."Item_Price='$itemprice' WHERE Item_Id='$itemid'";

If you're building the update string in php, you might as well just build it differently if it's empty:

$update = "UPDATE Item_t SET ".($itemname ? "Item_Name='$itemname', " : "")."Item_Price='$itemprice' WHERE Item_Id='$itemid'";
青萝楚歌 2024-12-26 07:01:38

尝试这样的事情。

$strSet = '';


//make sure to sanitize your inputs first, then do this....
if(Item_Name != ''){ $strSet .= 'Item_Name=\'$itemname\','};
if(Item_Price != ''){ $strSet .= 'Item_Price=\'$itemprice\','};

//removes trailing comma
$strSet = substr($strSet,0,-1);

//Update record
$update = "UPDATE Item_t SET " . $strSet . " WHERE Item_Id='$itemid'";
mysql_query($update);

try something like this.

$strSet = '';


//make sure to sanitize your inputs first, then do this....
if(Item_Name != ''){ $strSet .= 'Item_Name=\'$itemname\','};
if(Item_Price != ''){ $strSet .= 'Item_Price=\'$itemprice\','};

//removes trailing comma
$strSet = substr($strSet,0,-1);

//Update record
$update = "UPDATE Item_t SET " . $strSet . " WHERE Item_Id='$itemid'";
mysql_query($update);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文