简体   繁体   English

mysqli动态更新查询

[英]mysqli dynamic update query

With this query I update images fields in database: 通过此查询,我更新了数据库中的图像字段:

$sql = $connection->prepare('UPDATE ads img1 = ?, img2 = ?, img3 = ? WHERE id = ?');
$sql->bind_param("ssss", $nameImg1, $nameImg2, $nameImg3, $id);

But this work fine if user update all 3 images, not only one or two. 但是,如果用户更新所有3张图像,而不是仅更新一幅或两幅图像,此方法就可以正常工作。 if user change just one image, for example change img2 , img1 and img3 going to update to empty value because I use this condition: 如果用户仅更改一张图像,例如将img2img1img3更改为空值,因为我使用了这种情况:

$img1 = $_POST["img-1-val"];
$img2 = $_POST["img-2-val"];
$img3 = $_POST["img-3-val"];

if($img1 != 'NULL'){
$nameImg1 = $date.'_'.$adsid.'_1';
} 

if($img2 != 'NULL'){
$nameImg2 = $date.'_'.$adsid.'_2';
} 

if($img3 != 'NULL'){
$nameImg3 = $date.'_'.$adsid.'_3';
}

in html : html

<input type="hidden" name="img-1-val" value="NULL"/> 
<!-- this already has image -->
<input type="hidden" name="img-2-val"/>
<input type="hidden" name="img-3-val"/>

If each image has image, value is NULL if there is no image set, it is empty, if user change any image, it set base64 value. 如果每个图像都有图像,则如果未设置图像,则值为NULL ;如果用户更改任何图像,则将base64值设置为NULL

But the main problem is, I don't want to update any img field in database if $_POST return NULL as value, already it update all fields, img1, img2, img3,and this cause of removing previously data. 但是主要的问题是,如果$_POST返回NULL作为值,我不想更新数据库中的任何img字段,它已经更新了所有字段img1,img2,img3,这是删除先前数据的原因。 How can I update database field if value not equal to NULL? 如果值不等于NULL,如何更新数据库字段?


Consider these codes run in edit page. 考虑这些代码在编辑页面中运行。 Also my problem is mysqli query not if condition. 另外我的问题是mysqli查询不if条件。

You can make your update query dynamic like this : 您可以像这样使更新查询动态化:

/* function to build SQL UPDATE string */
function build_sql_update($table, $data, $where)
{
    $cols = array();

    foreach($data as $key=>$val) {
        if($val != NULL) // check if value is not null then only add that colunm to array
        {
           $cols[] = "$key = '$val'"; 
        }
    }
    $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where";

    return($sql);
}

In this way, only those columns will be updated which has valid values. 这样,只有那些具有有效值的列将被更新。

So, you have to just check with different variable with query. 因此,您只需要使用查询检查不同的变量即可。

This will check one by one image and if all the images have values then it will update all three images otherwise one by one. 这将一张一张地检查图像,如果所有图像都具有值,则它将更新所有三张图像,否则将一张一张地更新。 This may help. 这可能会有所帮助。

if((isset($img1)) && ($img1 != 'NULL')){
    $nameImg1 = $date.'_'.$adsid.'_1';
    $sql = $connection->prepare('UPDATE ads img1 = ? WHERE id = ?');
    $sql->bind_param("ss", $nameImg1, $id);
} 

if((isset($img2)) && ($img2 != 'NULL')){
    $nameImg2 = $date.'_'.$adsid.'_2';
    $sql = $connection->prepare('UPDATE ads img2 = ? WHERE id = ?');
    $sql->bind_param("ss", $nameImg2, $id);
} 

if((isset($img3)) && ($img3 != 'NULL')){
    $nameImg3 = $date.'_'.$adsid.'_3';
    $sql = $connection->prepare('UPDATE ads img3 = ? WHERE id = ?');
    $sql->bind_param("ss", $nameImg3, $id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM