如何为我的附加程序功能改进我的if else语句

now I would like to do a simple slave-gaming buy/sell function.

Now my database :T1, My data is:

+----------+------------+------------+-----------+------------+----------+
| uid      | exp        | lvl        | master    | slaveby    |bytime    |
+----------+------------+------------+-----------+------------+----------+
| 1        | 123        | 1          | 10        | 20         |12345     |
| 20       | 456        | 1          | 1         | 0          |12345     |
| 10       | 111        | 2          | 0         | 1          |12222     |
+------------------------------------------------------------------------+

uid is user ID. type int(11)

exp is user experience. type int(11)

lvl is user level. type int(11)

master means now the user is which uid's master. type int(11)

slaveby means now the user is which uid's slave. type int(11)

bytime is the time when user slaveby. type int(11)

In the table data, we can know row 1 was the user 1 was the user 10 master, but also was user 20's slave.

Now my coding run like this.

if ($_GET['mod'] == 'hireSlave'){
        mysql_query("UPDATE ".DB::table('game_goldwar_labour')." SET touid = '".$_GET['touid']."', bytime = '".$_GET[timestamp]."' WHERE uid = '".$_GET[uid]."'");
        mysql_query("UPDATE ".DB::table('game_goldwar_labour')." SET byuid = '".$_GET[uid]."', bytime = '".$_GET[timestamp]."' WHERE uid = '".$_GET['touid']."'");
};

But this coding the user only can be 1 user's master only.

I would like to change when user lvl 2, can be 2 slave's master, lvl3, can be 3 slave's master, maximun 5 slave, and also the bytime need to separate. Because I set when the bytime is reach the maximun time, the slave user will auto runaway. If this slave is in the second slot, will update the slave second slot as 0 (normally is user id of the slave, but because already runaway, so set to 0), after that, when the master hire slave when the slot is available.

slot available based on level and the slot was 0.

And also when user lvl1, cannot get 2 slave.

If I use if/else statement, will be a huge script.

So how to improve this? thank you.

You need to use a separate table for storing slaves, for example have the columns: uid slave bytime (you also don't need to store both slaveby and master at the same time, that's redundant). When inserting a new record, check for the master's level to make sure the count is less than the number of slaves, otherwise don't insert a new record. I'm guessing you'd need an UNIQUE constraint in slave if a slave can only have one master at one time.

For fetching a master's slaves simply do:

SELECT slave FROM slaves WHERE uid = {uid}

For fetching a slave's master simply do:

SELECT uid FROM slaves WHERE slave = {slaveid}

EDIT: Forgot to mention one thing, you really should do some input escaping and validation if you aren't already and use a DB layer like PDO to avoid SQL injections.