无法动态生成MYSQL查询

时间:2016-08-24 13:00:46

标签: php mysql

以下mysqli查询无效。我需要将SET变量值分配给blink''space或null。但问题是在SET之后如果我使用任何变量然后它不工作。请告诉我,我对这个查询做了什么错误?

$q = mysqli_query($conn, "UPDATE photo_gallery 
                          SET $name_of_image_to_del=' ' 
                          WHERE id='$row_id'");

2 个答案:

答案 0 :(得分:0)

首先检查值是否存在,然后传入该查询: -

if (isset($name_of_image_to_del) && $name_of_image_to_del != '') {
    $q = mysqli_query($conn, "UPDATE photo_gallery SET $name_of_image_to_del=' ' WHERE id='$row_id'");
}

答案 1 :(得分:0)

我认为您想要更新columnName的值,即Dynamic,

请查找以下代码:

<?php
    $conn = mysqli_connect("localhost","root","password","db");
    $columnName = 'columnName';
    $valueToCheck = 'makeMeBlank';
    $sql = "UPDATE tableName SET $columnName=' ' WHERE id='$valueToCheck'";
    $q = mysqli_query($conn, $sql);
?>