[英]Updating 4 SQL fields with PHP by user_id
Is this possible to update 4 different columns based on a person " user_id
" within the code? 是否可以根据代码中的人员“
user_id
”更新4个不同的列?
I know there are a few examples but I'm having trouble understanding them, if someone could give me an example of how this would be done, so I can change my code accordingly I would be very grateful. 我知道有一些示例,但是如果有人可以给我一个示例说明如何完成操作,我将很难理解它们,因此我可以相应地更改代码,我将不胜感激。
The code below works to update field1
, but how can I tailor it to upload field2
, field3
and field4
also? 下面的代码可用于更新
field1
,但是如何定制它以同时上载field2
, field3
和field4
?
<?php
include 'inc/db-connect2.php' ;
$dbname = "dtarg";
mysql_select_db($dbname);
$username = $_GET["username"];
$bianca = $_GET["bianca"];
$carla = $_GET["carla"];
$caroline = $_GET["caroline"];
$carrieanne = $_GET["carrieanne"];
$catherine = $_GET["catherine"];
$frazer = $_GET["frazer"];
$jason = $_GET["jason"];
$jay = $_GET["jay"];
$jordan = $_GET["jordan"];
$kim = $_GET["kim"];
$lindsay = $_GET["lindsay"];
$natasha = $_GET["natasha"];
$steph = $_GET["steph"];
$toby = $_GET["toby"];
$namequery = "SELECT * FROM users WHERE name='$username'";
$nameresult = mysql_query($namequery) or die(mysql_error());
while($namerow = mysql_fetch_array($nameresult)){
$userid = $namerow['user_id'];
$shift = $namerow['shift'];
}
$addrecord="UPDATE users
SET field1 = CASE user_id
WHEN '18' THEN '$bianca'
WHEN '19' THEN '$carla'
WHEN '20' THEN '$caroline'
WHEN '7' THEN '$carrieanne'
WHEN '16' THEN '$catherine'
WHEN '21' THEN '$frazer'
WHEN '15' THEN '$jason'
WHEN '5' THEN '$jay'
WHEN '11' THEN '$jordan'
WHEN '10' THEN '$kim'
WHEN '25' THEN '$lindsay'
WHEN '6' THEN '$natasha'
WHEN '1' THEN '$steph'
WHEN '4' THEN '$toby'
END
WHERE user_id IN (18,19,20,7,16,21,15,5,11,10,25,6,1,4)";
if (!mysql_query($addrecord))
{
die('Error: ' . mysql_error());
}
?>
EDIT 2 - This code seems to update field1, to the same value as field2? 编辑2-此代码似乎将field1更新为与field2相同的值?
$addrecord="UPDATE users
SET field1 = CASE user_id
WHEN '18' THEN '$bianca'
WHEN '19' THEN '$carla'
END ,
field2 = CASE user_id
WHEN '18' THEN '$bianca'
WHEN '19' THEN '$carla'
END
WHERE user_id IN (18,19,20,7,16,21,15,5,11,10,25,6,1,4)";
if (!mysql_query($addrecord))
{
die('Error: ' . mysql_error());
}
Yes, just add a comma and reuse the logic : 是的,只需添加一个逗号并重用逻辑即可:
SET field1 = CASE user_id
WHEN '18' THEN '$bianca'
WHEN '19' THEN '$carla'
.... END ,
field2 = CASE user_id
WHEN '18' THEN '$bianca'
WHEN '19' THEN '$carla'
.... END ,
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.