简体   繁体   English

从php更新MS sql服务器中的多行的问题

[英]Issue in updating multiple rows in MS sql server from php

I am trying to update multiple rows of a table via php using the below code : 我正在尝试使用以下代码通过php更新表的多行:

$sql ="update [TableName] set status = 1 where producturl in (?)";
$param = array($purl);
$response = sqlsrv_query($conn,$sql,$param);

where $purl = ('h','a','b','c','d') 其中$purl = ('h','a','b','c','d')

The above code is not updating anything. 上面的代码没有更新任何内容。

Yes, because you are inserting one single parameter. 是的,因为您要插入一个参数。 The query translates to 查询翻译成

update [TableName] 
set status = 1
where producturl in ("'h','a','b','c','d'")

So you could use multiple parameters 所以你可以使用多个参数

update [TableName] 
set status = 1
where producturl in (?,?,?,?,?)

or build a query string 或建立查询字串

$arr = join(',',$purl); 

$sql = "update [TableName] 
        set status = 1
        where producturl in ($arr)";

But then watch out for SQL injection. 但是,当心SQL注入。 Always escape user input. 始终避免用户输入。

I did it like below : 我这样做如下:

$purl = "('h','a','b','c','d')";
$sql ="update [TableName] set status = 1 where producturl in ".$purl;  
$response = sqlsrv_query($conn,$sql);

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

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