简体   繁体   English

在更新参数中包含变量的SQL

[英]SQL with variables in update argument

Is there a way to make a variable change the execute destination for an SQL command? 有没有办法使变量更改SQL命令的执行目标?

Basically, my question is, if it's possible through a PHP $variable to change the update destination for an SQL argument? 基本上,我的问题是,是否可以通过PHP $variable更改SQL参数的更新目标?

In this case, I have tried to update the column score by using a PHP $Variable . 在这种情况下,我尝试使用PHP $Variable更新列分数。

How will I use this?, I will store the specific score destination "Destination, what row on the database" on the file that makes the update request, so I can avoid different config files "With config files, I mean different files for each game, example. 我将如何使用它?我将特定分数目标“目的地,数据库的哪一行”存储在发出更新请求的文件上,因此我可以避免使用不同的配置文件“对于配置文件,我的意思是每个文件都不同游戏,例如。

One game needs to save to Row1, I create a game config file that specifies where I will save my information to, then I have another game that will need to save to row2, I cant use the first game config file, therefore, a new config file needs to be made for that game. 一个游戏需要保存到Row1,我创建了一个游戏配置文件,该文件指定了将信息保存到的位置,然后我有另一个游戏需要保存到Row2,我无法使用第一个游戏配置文件,因此,需要为该游戏制作配置文件。

Example on storage destination variable: $scoretype = "x" // Where X is the destination . 关于存储目标变量的示例: $scoretype = "x" // Where X is the destination

Why do I want to do this? 我为什么要这样做? I'm currently working on a game that has several different scores and therefore many different save destinations. 我目前正在开发一款游戏,该游戏具有多个不同的分数,因此具有许多不同的保存目标。

One destination and score for game1, one for game2 etc, this is a crucial part of the functionality of the website, hence my interesting question, to skip unnecessary server filling with different PHP config files for each game and for simplicity sake. Game1的一个目标和得分,game2的一个目标和得分,这是网站功能的关键部分,因此,我提出了一个有趣的问题,即跳过每个游戏不必要的服务器,并为每个游戏添加不同的PHP配置文件,以简化操作。

What I have been trying: 我一直在尝试:

$id = $rid['id'];
$scoretype = "xpscore"; // Scoretype is where i will be saving the score in the database
$score = '1500'; // enabled for testing purposes
//$score = $_POST['uscore']; // disableded for testing purposes
$sql = "UPDATE users SET score='$score' WHERE id='$id' AND score='$scoretype'";  // I know there are two definitions for score here, just dont know how else i should declare where and what to save. 

Is this even possible to achieve? 这甚至有可能实现吗?

I have not seen any tutorial or explanation explaining a scenario like this, therefore I'm curious if this actually could be achieved? 我没有看到任何教程或解释来解释这种情况,因此我很好奇这是否可以实现?

Sidenote I have a backup plan if this doesn't work or isn't achievable. 旁注如果此方法不可行或无法实现,则我有一个备份计划。 Just wanted to see if there is a better alternative to making several "config.php" files just for changing the update destination which in this case will be where the score will be saved. 只是想看看是否有更好的方法来制作多个“ config.php”文件,仅用于更改更新目标,在这种情况下将保存分数。

Shouldn't you be having another field for scoretype? 您不应该为scoretype设置另一个字段吗?

UPDATE users SET score='$score' WHERE id='$id' AND scoretype='$scoretype'

Or store scores to another table where you have columns for user id, score and score type. 或将分数存储到另一个表中,该表中具有用户ID,分数和分数类型的列。

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

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