如何更新MySQL查询中SET子句依赖于变量的数据库中的表?它可以是一个$ var,两个或更多。
这是我的前任。代码:
$name = 'comp_name = "'.$nume.'",';
$large = 'logolarge = "'.$linklogolarge.'",';
$small = 'logosmall = "'.$linklogosmall.'",';
...............................
$sql = 'UPDATE company
SET
'.$name.'
'.$large.'
'.$small.'
WHERE id_comp = 43 ';
问题是UPDATE查询的正常语法是在SET .......之间值之间需要逗号“,”
离。
UPDATE table
SET
col1 = x ,
col2 = y ,
col3 = z
WHERE id = 4
并且在WHERE之前不需要一个.....(例如“z”之后)
那么如何让sql查询接受那些$ var的不同组合...(只有'。$ name。'或'。$ name。'和'。$ large。'或者只有最后两个'。 $ large。'和'。$ small。'....等等)
基本上是3个var之间的任何组合......并且可能组合超过3个var。
答案 0 :(得分:1)
$name = "comp_name = '$nume'";
$large = "logolarge = '$linklogolarge'";
$small = "logosmall = '$linklogosmall'";
$sql = "UPDATE company SET " . $name;
if( ! is_null( $linklogolarge ) ) $sql = $sql . ", " . $large;
if( ! is_null( $linklogosmall ) ) $sql = $sql . ", " . $small;
$where = " WHERE id_comp = 43"; /* change this if required*/
$sql = $sql . $where;
echo $sql;