SQL:仅在输入不为空时进行编辑

时间:2017-12-20 17:37:02

标签: php mysql

我目前正在处理简单的圣诞礼物数据库:)我的编辑功能有问题。当用户选择现有礼品进行编辑(按ID)并输入新值(例如价格)时,我只想改变价格,其他所有内容保持不变。我尝试使用函数IFNULL,但我的代码没有像我预期的那样工作。每当我获得价格的新价值时,其他字段都会被删除。

我的代码(Iam使用MySQL):

            else if($_REQUEST['btn_submit']=="Edit")
            {
                    $gifts_id = $_POST["gifts_id"];

                    $year = $_POST["year"];
                    $whom = $_POST["whom"]; 
                    $category = $_POST["category"]; 
                    $what = $_POST["what"]; 
                    $shop = $_POST["shop"]; 
                    $url = $_POST["url"]; 
                    $price = $_POST["price"]; 
                    $note = $_POST["note"]; 
                    $status = $_POST["status"];                  

                    Db::query("
                        UPDATE `gifts` 
                        SET
                            `year` = ifnull('$year',`year`),
                            `whom` = ifnull('$whom',`whom`),
                            `category` = ifnull('$category',`category`),
                            `what` = ifnull('$what',`what`),
                            `shop` = ifnull('$shop',`shop`),
                            `url` = ifnull('$url',`url`),
                            `price` = ifnull('$price',`price`),
                            `note` = ifnull('$note',`note`),
                            `status` = ifnull('$status',`status`) 
                        WHERE
                            `gifts_id` = '$gifts_id';
                              ");

                    echo("<p>Gift with ID:'$gifts_id' successfully updated</p>");
            }

感谢您的回答!

PS:我的代码只是为了好玩,所以请怜悯:)

2 个答案:

答案 0 :(得分:0)

如果您想要正确编辑您的值,首先应使用旧值填充所有输入,以便用户可以编辑它们或保留原样。然后,您可以在调用sql之前检查所有值是否为null,如下所示:

else if($_REQUEST['btn_submit']=="Edit")
            {
                    $gifts_id = $_POST["gifts_id"];

                    $year = $_POST["year"];
                    $whom = $_POST["whom"]; 
                    $category = $_POST["category"]; 
                    $what = $_POST["what"]; 
                    $shop = $_POST["shop"]; 
                    $url = $_POST["url"]; 
                    $price = $_POST["price"]; 
                    $note = $_POST["note"]; 
                    $status = $_POST["status"];                  
if(!empty($gifts_id)&&!empty($year)&&!empty($whom)&&!empty($category)&&!empty( $what)&&!empty($shop)&&!empty($url )&&!empty($price)&&!empty($note)&&!empty($status))
{

Db::query("
                        UPDATE `gifts` 
                        SET
                            `year` = ifnull('$year',`year`),
                            `whom` = ifnull('$whom',`whom`),
                            `category` = ifnull('$category',`category`),
                            `what` = ifnull('$what',`what`),
                            `shop` = ifnull('$shop',`shop`),
                            `url` = ifnull('$url',`url`),
                            `price` = ifnull('$price',`price`),
                            `note` = ifnull('$note',`note`),
                            `status` = ifnull('$status',`status`) 
                        WHERE
                            `gifts_id` = '$gifts_id';
                              ");

                    echo("<p>Gift with ID:'$gifts_id' successfully updated</p>");
            }
else
{
echo("<p>Gift with ID:'$gifts_id' was not updated, please check your data</p>");
}

答案 1 :(得分:0)

IFNULL只测试特殊的NULL值,引用的字符串永远不会为空。您应该将字符串与''进行比较。

                Db::query("
                    UPDATE `gifts` 
                    SET
                        `year` = if('$year' = '',`year`, '$year'),
                        `whom` = if('$whom' = '',`whom`, '$whom'),
                        ...
                    WHERE
                        `gifts_id` = '$gifts_id';
                          ");

另一种选择是动态构建查询。

$assign_array = array();
foreach (array('year', 'whom', 'category', ...) AS $field) {
    if ($_POST[$field] !== '') {
        $assign_array[] = "`$field` = '{$_POST[$field]}'";
    }
}
$assign_string = implode(',', $assign_array);
Db::query("
    UPDATE `gifts` 
    SET $assign_string   
    WHERE  `gifts_id` = '$gifts_id';");

但请注意,这很容易受到SQL注入的攻击。如果您的DB API允许您创建准备好的查询并提供值数组,那么您应该这样做。您可以采用与此类似的方式构建参数化查询和值数组。