簡體   English   中英

如何僅使用非空用戶輸入字段更新 MySQL 數據庫

[英]How to update MySQL database using only non empty user input fields

我有一個數據庫,我正在嘗試根據某些用戶輸入表單進行更新。 我正在使用以下代碼更新 MySQL 數據庫。

$query = "UPDATE supervisorupdate SET shift1PinCount =?, shift2PinCount =?, shift3PinCount =?, shift4PinCount =? WHERE Code=?";

$stmt = mysqli_stmt_init($conn);

if(!mysqli_stmt_prepare($stmt, $query)){
   echo "updating supervisorupdate table when click save button SQL statement failed";
}else{
   mysqli_stmt_bind_param($stmt,"iiiis", $shift1PinCount, $shift2PinCount, $shift3PinCount, $shift4PinCount, $Code);
   mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt);
}

這里$shift1PinCount$shift2PinCount$shift3PinCount$shift4PinCount是我從用戶輸入表單中獲取的一些$_POST變量

我需要的

假設用戶沒有在$shift1PinCount字段中輸入任何內容,則查詢不應將任何內容更新到 MySQL 數據庫的shift1PinCount列中。

我的問題

使用我的查詢,無論用戶是否鍵入某些內容或在用戶輸入表單中將其留空,它始終會更新所有 MySQL 列。 有誰知道該怎么做?

嘗試將 if 條件放在帖子數據上,例如

<?php
     if(isset($_POST["shift1"]))
{

    $query = "
        Update product SET shift1 = ?,
    ";
    if(isset($_POST["shift2"]))
    {
        $query .= "
         shift2 = ? ,
        ";
    }
    if(isset($_POST["shift3"]))
    {
        $query .= "
         shift3 = ? ,
        ";
    }if(isset($_POST["shift4"]))
    {
        $query .= "
         shift4 = ? ,
        ";
    }
    $query .=" Where Code=?";
    $stmt = mysqli_stmt_init($conn);

    if(!mysqli_stmt_prepare($stmt, $query)){
       echo "updating supervisorupdate table when click save button SQL statement failed";
    }else{
       mysqli_stmt_bind_param($stmt,"iiiis", $_POST["shift1"], $_POST["shift1"], $_POST["shift1"], $_POST["shift1"], $Code);
       mysqli_stmt_execute($stmt);
       $result = mysqli_stmt_get_result($stmt);
    }
}
?>

在嘗試了一些方法后,我可以通過以下方式完成它。 但我相信有更好的方法可以做到。 如果有人知道更好的方法,請發布

 if(isset($_POST['shift1PinCount']) && !empty($_POST['shift1PinCount'])){ pinCountUpdateQuery("shift1PinCount", $_POST['shift1PinCount'], $Code); } if(isset($_POST['shift2PinCount']) && !empty($_POST['shift2PinCount'])){ pinCountUpdateQuery("shift2PinCount", $_POST['shift2PinCount'], $Code); } if(isset($_POST['shift3PinCount']) && !empty($_POST['shift3PinCount'])){ pinCountUpdateQuery("shift3PinCount", $_POST['shift3PinCount'], $Code); } if(isset($_POST['shift4PinCount']) && !empty($_POST['shift4PinCount'])){ pinCountUpdateQuery("shift4PinCount", $_POST['shift4PinCount'], $Code); }

然后使用下面的函數

 function pinCountUpdateQuery($shiftPinCountDb, $shiftPinCountUserForm, $Code){ global $conn; $query = "UPDATE supervisorupdate SET ".$shiftPinCountDb." =? WHERE Code=?"; $stmt = mysqli_stmt_init($conn); if(!mysqli_stmt_prepare($stmt, $query)){ echo "updating supervisorupdate table when click save button SQL statement failed"; }else{ mysqli_stmt_bind_param($stmt,"is", $shiftPinCountUserForm, $Code); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); } }

偽代碼

param1 = req get param1
param2 = req get param2
.
.
paramN = req get paramN

$query = "UPDATE supervisorupdate SET "
bool can_be_updated = false
# now add prepare the rest of sql for inputs
for range of params count to be updated
    if current param is valid then
        if not can_be_updated then
            can_be_updated = true
        end if
        if current param number is > 1 < param count then
            query += ", "
        end if
        query += 'current param name = ?'
    end if
end loop

# now complete sql
if can_be_updated then 
    $query += ' WHERE Code=?'
    .
    .
    # bind sql with valid param inputs like one of the following
    # option 1: if only 2 are valid
    call rel_mysql_func_to_bind_params($stmt,"iiiis", valid param 1, valid param M, $Code);
    # or
    # option N: if all are valid
    call rel_mysql_func_to_bind_params($stmt,"iiiis", $shift1PinCount, $shift2PinCount, $shift3PinCount, $shift4PinCount, $Code);

    # now execute your stmt
    relevant_mysql_func_to_execute($stmt);
else
    # what else you want, perform here
end if

其他
請參閱為 Java @ 回答的類似要求
如何使用 java.sql 中 where 子句的動態計數執行 mysql 語句

一種選擇是使用COALESCE()如下:

UPDATE supervisorupdate SET 
    shift1PinCount = COALESCE(?, shift1PinCount),
    shift2PinCount = COALESCE(?, shift2PinCount),
    shift3PinCount = COALESCE(?, shift3PinCount),
    shift4PinCount = COALESCE(?, shift4PinCount)
WHERE Code = ?

這句話是:如果沒有為給定列的輸入提供值,則保留現有值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM