繁体   English   中英

SQL更新具有不同值的多行

[英]SQL update multiple rows with different values

编辑:我重写了我的问题,以包括更多详细信息:

这是从用户数据库生成结果页面的代码的快照:

userdetails.php

$currenttechnicians = "SELECT * FROM ahsinfo ORDER BY FirstName";
$currenttechniciansresults = sqlsrv_query($conn,$currenttechnicians) or die("Couldn't     execut query");

while ($techniciandata=sqlsrv_fetch_array($currenttechniciansresults)){

echo "<tr align=center>";
echo "<td height=35 background=largebg.gif style=text-align: center valign=center>";

echo "<input type=hidden name=\"ID\" value=";
echo $techniciandata['ID'];
echo ">";
echo "<input type=textbox name=\"FirstName\" value=";
echo $techniciandata['FirstName'];
echo "> ";
echo "</td><td height=35 background=largebg.gif style=text-align: center valign=center>";
echo "<input type=textbox name=\"LastName\" value=";
echo $techniciandata['LastName'];
echo "> ";
echo "</td><td height=35 background=largebg.gif style=text-align: center valign=center>";
echo "<input type=textbox size=40 name=\"SIPAddress\" value=";
echo $techniciandata['SIPAddress'];
echo "> ";
echo "</td><td height=35 background=largebg.gif style=text-align: center valign=center>";
echo "<input type=textbox name=\"MobileNumber\" value=";
echo $techniciandata['MobileNumber'];
echo "> ";
echo "</td><td height=35 background=largebg.gif style=text-align: center valign=center>";

?>

这是处理表单提交的代码:(同样,只是更新数据库的SQL查询。我删除了数据库连接字符串)。

edituserdetails.php

<?PHP
//Add the new users details to the database
$edituser = " UPDATE ahsinfo SET FirstName='{$_POST['FirstName']}',     LastName='{$_POST['LastName']}', SIPAddress='{$_POST['SIPAddress']}',     MobileNumber='{$_POST['MobileNumber']}' WHERE ID='{$_POST['ID']}' ";
$editresult = sqlsrv_query($conn,$edituser) or die("Couldn't execute query to update     database");
?>

我正在尝试使用用户在userdetails.php页面上进行的任何更改来更新整个数据库(这是一个小型数据库)。

您在字符串值周围缺少引号,并在其中一个POST变量上使用了花括号而不是方括号:

UPDATE ahsinfo 
SET FirstName="{$_POST['FirstName']}", 
    LastName="{$_POST['LastName']}", 
    EmailAddress="{$_POST['EmailAddress']}", 
    MobileNumber="{$_POST['MobileNumber']}" 
WHERE ID={$_POST['ID']}

如果您检查代码中的错误,则将捕获此错误。

仅供参考,您对SQL注入持开放态度

暂无
暂无

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

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