Mysqli 更好的方法来更改使用的列?
Mysqli better way to change what column working with?
所以我的代码需要有一个重量跟踪器,并且需要在一个数据点中保持至少 20 个重量。因为我不能用“?”动态改变它。该位置我有 20 个 if () else if 语句能够做同样的事情。只是想知道是否有更好的方法来做到这一点,或者这是唯一的方法吗?
代码开头:
function addWeight($conn, $location, $weight,$dt){
//select the two variables changing
if($location == "Weight1"){
$sql ="UPDATE userweight SET Weight1=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee1=? WHERE userId = ?;";
}elseif($location == "Weight2"){
$sql ="UPDATE userweight SET Weight2=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee2=? WHERE userId = ?;";
}elseif($location == "Weight3"){
$sql ="UPDATE userweight SET Weight3=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee3=? WHERE userId = ?;";
}elseif($location == "Weight4"){
$sql ="UPDATE userweight SET Weight4=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee4=? WHERE userId = ?;";
}
.....
不要这样存储数据。这只是自找麻烦。而是规范化您的模式并以关系方式存储数据。
如果您受困于当前的设计,可以通过动态构建 SQL 来解决这个问题。请务必使用硬编码值列表验证列名是否正确。
function addWeight(mysqli $conn, $location, $weight, $dt)
{
$columns = [
"Weight1" => 'timee1',
"Weight2" => 'timee2',
"Weight3" => 'timee3',
"Weight4" => 'timee4',
];
if (!isset($columns[$location])) {
throw new \RuntimeException("Invalid location");
}
$sql = "UPDATE userweight SET {$location}=? WHERE userId = ?;";
$sql2 = "UPDATE userweight SET {$columns[$location]}=? WHERE userId = ?;";
}
考虑使用两个表,并将位置更改为仅一个数字:
Table 1: userweight
userId | location_nr | weight |
1 | 1 | 60 |
1 | 8 | 93 |
Table 2: usertime
userId | location_nr | time |
1 | 1 | 2020-05-03 |
1 | 8 | 2020-09-18 |
现在,根据 user_id 和位置,您可以使用如下查询:
UPDATE userweight SET weight = ? WHERE userId = ? and location_nr= ?
UPDATE usertime SET timee= ? WHERE userId = ? and location_nr= ?
所以我的代码需要有一个重量跟踪器,并且需要在一个数据点中保持至少 20 个重量。因为我不能用“?”动态改变它。该位置我有 20 个 if () else if 语句能够做同样的事情。只是想知道是否有更好的方法来做到这一点,或者这是唯一的方法吗?
代码开头:
function addWeight($conn, $location, $weight,$dt){
//select the two variables changing
if($location == "Weight1"){
$sql ="UPDATE userweight SET Weight1=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee1=? WHERE userId = ?;";
}elseif($location == "Weight2"){
$sql ="UPDATE userweight SET Weight2=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee2=? WHERE userId = ?;";
}elseif($location == "Weight3"){
$sql ="UPDATE userweight SET Weight3=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee3=? WHERE userId = ?;";
}elseif($location == "Weight4"){
$sql ="UPDATE userweight SET Weight4=? WHERE userId = ?;";
$sql2 ="UPDATE userweight SET timee4=? WHERE userId = ?;";
}
.....
不要这样存储数据。这只是自找麻烦。而是规范化您的模式并以关系方式存储数据。
如果您受困于当前的设计,可以通过动态构建 SQL 来解决这个问题。请务必使用硬编码值列表验证列名是否正确。
function addWeight(mysqli $conn, $location, $weight, $dt)
{
$columns = [
"Weight1" => 'timee1',
"Weight2" => 'timee2',
"Weight3" => 'timee3',
"Weight4" => 'timee4',
];
if (!isset($columns[$location])) {
throw new \RuntimeException("Invalid location");
}
$sql = "UPDATE userweight SET {$location}=? WHERE userId = ?;";
$sql2 = "UPDATE userweight SET {$columns[$location]}=? WHERE userId = ?;";
}
考虑使用两个表,并将位置更改为仅一个数字:
Table 1: userweight
userId | location_nr | weight |
1 | 1 | 60 |
1 | 8 | 93 |
Table 2: usertime
userId | location_nr | time |
1 | 1 | 2020-05-03 |
1 | 8 | 2020-09-18 |
现在,根据 user_id 和位置,您可以使用如下查询:
UPDATE userweight SET weight = ? WHERE userId = ? and location_nr= ?
UPDATE usertime SET timee= ? WHERE userId = ? and location_nr= ?