简体   繁体   中英

SQL with variables in update argument

Is there a way to make a variable change the execute destination for an SQL command?

Basically, my question is, if it's possible through a PHP $variable to change the update destination for an SQL argument?

In this case, I have tried to update the column score by using a 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.

Example on storage destination variable: $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.

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.

Shouldn't you be having another field for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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