MSSQL UPDATE SELECT和SET Query无法正常工作

时间:2016-07-14 04:18:08

标签: php sql sql-server

 var temp = [];

 function addSong(id) {

     var button = $('#' + id + ''); // Button that triggered the modal
     var file = button.data('file'); // Extract info from data-* attributes
     var thumb = button.data('thumb'); // Extract info from data-* attributes
     var trackName = button.data('track_name'); // Extract info from data-* attributes
     var trackArtist = button.data('track_artist'); // Extract info from data-* attributes
     var trackAlbum = button.data('track_album'); // Extract info from data-* attributes

     var tempObj = {
         file: file,
         thumb: thumb,
         trackName: trackName,
         trackArtist: trackArtist,
         trackAlbum: trackAlbum
     };

     temp.push(tempObj);

     $("#player").jAudio({
         playlist: temp
     });
 }

pic变量仅用于测试。无论如何,我可以确认用户名=:用户名是真的,这只是重置语句的问题。正如我所说,除了声明本身之外,没有任何问题。我在mssql 2008数据库上使用php7。感谢您的投入!

EDIT 用这个替换代码,仍然不起作用:

$query = $database->prepare("SELECT * FROM Users WHERE username=:username SET profilePicturePath=:pic");
$query->bindParam(":pic", $_FILES['profilePic']['name']);
$pic = "hellow";
$query->bindParam(":username", $pic);
$query->execute();

从用户中选择所有作品,但下面的内容也不起作用:

$query = $database->prepare("SELECT * FROM Users WHERE username=@username    SET profilePicturePath=@pic");
$pic = "hellow";
$query->bindParam("@pic", $pic);
$query->bindParam("@username", $_SESSION['Username']);
$query->execute();

2 个答案:

答案 0 :(得分:0)

尝试查询

"UPDATE Users SET profilePicturePath=@pic WHERE username=@username"

答案 1 :(得分:0)

根据MSDN页面,SQL Server的SELECT语法不允许在SELECT语句中进行更新:https://msdn.microsoft.com/en-us/library/ms189499(v=sql.110).aspx。 你必须做两个单独的陈述,一个用于选择:

$query = $database->prepare("SELECT * FROM Users WHERE username=:username");
$pic = "hellow";
$query->bindParam(":username", $pic);
$query->execute();

和另一个更新:

$query = $database->prepare("UPDATE Users SET profilePicturePath=:pic where username = :username");
$query->bindParam(":pic", $_FILES['profilePic']['name']);
$query->bindParam(":username", $pic);
$query->execute();

此外,要使更新生效,请确保目标数据库中的用户具有UPDATE权限。您可以通过将用户添加到目标数据库中的db_datawrite数据库角色来授予更新和插入权限:

USE target_database; ALTER ROLE db_datawriter ADD MEMBER user_name;