Jump to content

Need help to convert SQL to ODBC.


rilee16

Recommended Posts

Hello i'm here to ask some help about a project game , here is a reborn system.

But the problem is i can't use MSSQL script to do this , i need to convert into ODBC. I hope i can get some help here..

 

Here is the sql script :

<?php

        if (isset($_POST['submitchange'])) {

                $user = $_SESSION['username'];

                $CharName = $_POST['charname'];

               

                $mssql->dbClose();

                $mssql->dbOpen($conf->accountDbName);

               

                $TB_User = $mssql->dbQuery("select * FROM TB_User where StrUserID = '$user'");

                while($row = sqlsrv_fetch_array($TB_User, SQLSRV_FETCH_ASSOC)) {

                                $JID = $row['JID'];

                                }

 

                $mssql->dbClose();

                $mssql->dbOpen($conf->shardDbName);

               

                $_Char = $mssql->dbQuery("select * FROM _Char where Charname16 = '$CharName'");

                while($row = sqlsrv_fetch_array($_Char, SQLSRV_FETCH_ASSOC)) {

                                $CharID = $row['CharID'];

                                $Strength = $row['Strength'];

                                $Intellect = $row['Intellect'];

                                $Reborns = $row['Reborns'];

                                $CurLevel = $row['CurLevel'];

                                }

 

                $_User = $mssql->dbQuery("SELECT COUNT(*) FROM _User WHERE UserJID = '$JID' and CharID = '$CharID'");

                if( sqlsrv_fetch( $_User ) === false) {

                        die( print_r( sqlsrv_errors(), true));

                }

                $_User_Row = sqlsrv_get_field( $_User, 0);

               

               

                if ($_User_Row = 1) { 

                        if ($Reborns == 0 and $CurLevel == 120)

                        {

                                $_0 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 30 , Intellect = 30 , RemainSkillPoint = 0  , ExpOffset = 0,RemainStatPoint = 0, Reborns = 1 WHERE CharName16 = '$CharName'");

                                $_0_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_0_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_0_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_0_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_0_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_0_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_0_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_0_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_0_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_0_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_0_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_0_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_0_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_0_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_0_15 = $mssql->dbQuery("UPDATE _CharSkillmMstery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 1 and $CurLevel == 120)

                        {

                                $_1 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 40 , Intellect = 40 , RemainSkillPoint = 0 , Reborns = 2 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");

                                $_1_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_1_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_1_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_1_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_1_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_1_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_1_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_1_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_1_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_1_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_1_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_1_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_1_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_1_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_1_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 2 and $CurLevel == 120)

                        {

                                $_2 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 50 , Intellect = 50 , RemainSkillPoint = 0 , Reborns = 3 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");     

                                $_2_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_2_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_2_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_2_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_2_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_2_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_2_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_2_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_2_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_2_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_2_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_2_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_2_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_2_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_2_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 3 and $CurLevel == 120)

                        {

                                $_3 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 60 , Intellect = 60 , RemainSkillPoint = 0 , Reborns = 4 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");     

                                $_3_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_3_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_3_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_3_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_3_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_3_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_3_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_3_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_3_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_3_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_3_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_3_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_3_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_3_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_3_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 4 and $CurLevel == 120)

                        {

                                $_4 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 70 , Intellect = 70 , RemainSkillPoint = 0 , Reborns = 5 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");     

                                $_4_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_4_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_4_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_4_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_4_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_4_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_4_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_4_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_4_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_4_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_4_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_4_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_4_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_4_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_4_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 5 and $CurLevel == 120)

                        {

                                $_5 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 80 , Intellect = 80 , RemainSkillPoint = 0 , Reborns = 6 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");     

                                $_5_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_5_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_5_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_5_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_5_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_5_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_5_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_5_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_5_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_5_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_5_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_5_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_5_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_5_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_5_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif ($Reborns == 6 and $CurLevel == 120)

                        {

                                $_6 = $mssql->dbQuery("UPDATE _Char set CurLevel = 1 , MaxLevel = 1 , Strength = 90 , Intellect = 90 , RemainSkillPoint = 0 , Reborns = 7 , ExpOffset = 0 , RemainStatPoint = 0 WHERE CharName16 = '$CharName'");     

                                $_6_1 = $mssql->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");

                                $_6_2 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");

                                $_6_3 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");

                                $_6_4 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");

                                $_6_5 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");

                                $_6_6 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");

                                $_6_7 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");

                                $_6_8 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");

                                $_6_9 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");

                                $_6_10 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");

                                $_6_11 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");

                                $_6_12 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");

                                $_6_13 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");

                                $_6_14 = $mssql->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");

                                $_6_15 = $mssql->dbQuery("UPDATE _CharSkillmastery set Level = 0 WHERE CharID = '$CharID'");

                        }

                        elseif($Reborns == 7)

                        {     

                                echo "Du hast bereits 7 Reborns ! ";

                                }

                        if ( $CurLevel !== 120)

                        {

                        echo 'Du bist nicht level 120 ! ';     

                        }

       

                        }else{ 

                        echo "Das ist nicht dein Character!";

                }

       

        } else {

                echo '<div class="page-body newsblock">';

                echo '<div class="news-title">';

                echo 'Reborn ur Char:';

                echo '</div>';

                echo '<div class="news-content">';

                echo '<form method="post">';

                echo 'Charactername:<br />';

                echo '<input onfocus="clearText(this);" style="background: rgba(0,0,0,0.5);width:220px;border-radius: 6px 6px 6px 6px;" type="text" maxlength="16" name="charname" value="charactername" /><br />';

                echo '<br />';

                echo '<input style="height:32px;vertical-align:middle;" class="button" type="submit" value="Submit" name="submitchange" />';

                echo '</form>';

                echo '</div>';

                echo '</div>';

        }

?>

 

<?php

        class mssql {

                var $config = "config.inc.php";

                var $sqlLink;

                var $sqlQuery;

                var $rowCount;

                var $arr;

               

                function dbOpen($dbName) {

                        if (!$this->sqlLink) {

                                include_once("$this->config");

                                global $mssql_server;

                                $db = array("Database"=>"$dbName");

                                $this->mssql_server = $mssql_server;   

                                $this->sqlLink = sqlsrv_connect($mssql_server, $db);

                                if (!$this->sqlLink) {

                                        echo "Error: Connection to the database couldn't be established. <br />";

                                }

                                return;

                        } else {

                                exit;

                        }

                }

                function dbClose() {

                        sqlsrv_close($this->sqlLink);

                }

                function dbQuery($string) {

                        $this->sqlQuery = sqlsrv_query($this->sqlLink, $string);

                        if(!$this->sqlQuery){

                                echo "Error: Query execution failed. <br />";

                                die( print_r( sqlsrv_errors(), true));                         

                        }

                        return $this->sqlQuery;

                }

        }

?>

 

<?php

        include("mssql.class.php");

 

        $mssql_server = "\SQLEXPRESS";

       

        class Config {

                var $shardDbName = "db"; //shard database name

                var $accountDbName = "db"; // account database name

Link to comment
Share on other sites

Concerning converting your code -

 

1) The specific sqlsrv_ functions that you still have in your main code should be replaced will calls to class methods that perform the same function. This will allow you to more simply convert your code by replacing your existing class with the one you write that uses ODBC functions.

 

2) I would also rename all the $mssql references to just $db to make the code generic.

 

3) You need to eliminate the use of the GLOBAL keyword in your class and instead define a constructor and pass the server specific "\SQLEXPRESS" value into the class as a call time parameter when you create an instance of your class - i.e. $db = new sqlsrv_db_class("\SQLEXPRESS");

 

I also have a number of comments about your actual code that have nothing to do with converting it. Most of these 'fixes' will improve the performance of your code, make your code easier to read, write, modify, and maintain, reduce the amount of code you have, and in one case make your code work the way you think -

 

1) You should not close and open a database connection just to change databases. It takes a significant amount of time to open a connection. You should instead issue a "USE dbname" query.

 

2) You should have all the related tables that make up your application in ONE database. This would eliminate the need to keep switching between database and keep track of the which database you are currently using.

 

3) You should store the user's 'JID' in a session variable when he logs in so that you don't need to specifically query for it every time a page gets requested.

 

4) The SELECT queries in your code expect to match only one row. There's no point in using a where(){} loop to retrieve that one row of data. Just fetch the row.

 

5) Your form should list the available character names either using a select menu or radio buttons so that all you need to do is pick the character you want. Your form should also submit the CharID of the character, not the character name. Passing less data around and using an integer, that would also be an index in your tables, will make your queries execute faster.

 

6) The following code only has one = sign and does not work like you expect (it is always true) - if ($_User_Row = 1) {. You need two == signs to test if $_User_Row == 1.

 

7) Assuming that your form submits the CharID instead of the CharName, the query to get the character data in _Char should be inside the logic that has tested if the user actually has the character. This query is also referencing some column values that are not used in the posted code. Excess lines of code should be removed or commented out so as to avoid confusion later when you or someone else tries to read your code. You should consistently use the CharID through out the code and queries.

 

8) You need to make your application "data driven". What this means is you define a data structure (array, query from a database table) that determines what your code does and you write general purpose code. Your blocks of repetitive code that has 'Strength' and 'Intellect' values hard-coded into them, should be replaced with one set of that code that uses a data structure to define what values get used.

 

9) If a multi-value INSERT query is available, you should use that instead of your list of 13 separate insert queries. The data for those insert queries should also be defined in a data structure (see item #8 above.)

 

10) Your database class is including a config file that it does not use and is assigning to $this->mssql_server where there is no class property with that name.

 

11) You also have no validation of external data and little or no logic in your code to test for errors or to test if a query matched any row(s).

 

The following incomplete and untested code SHOWS many of the things I suggested in the list above -

 

<?php

$db = new sqlsrv_db_class("\SQLEXPRESS");

if (isset($_POST['submitchange'])) {
$user = $_SESSION['username'];
$JID = $_SESSION['JID'];
$CharID = $_POST['CharID']; // assumes your form actually submits the CharID from a select menu or radio buttons

$db->dbQuery("USE {$conf->shardDbName}"); // select the correct database. If you only have one database, no need to do this at all.

// test if this user has the requested character
$_User = $db->dbQuery("SELECT COUNT(*) FROM _User WHERE UserJID = '$JID' and CharID = '$CharID'");
if( sqlsrv_fetch( $_User ) === false) {
	die( print_r( sqlsrv_errors(), true));
}

$_User_Row = sqlsrv_get_field( $_User, 0); // get the query count value
if ($_User_Row == 1) { // note: == equal signs for a comparison
	// get the character information
	$_Char = $db->dbQuery("select * FROM _Char where CharID = '$CharID'");
	$row = sqlsrv_fetch_array($_Char, SQLSRV_FETCH_ASSOC);
	$Reborns = $row['Reborns']; // used
	$CurLevel = $row['CurLevel']; // used

	// define values to use when a character is reborn, the index 0...6 is the current $Reborns value
	// you would normally define this data in an included configuration file (or get it from a database table)
	$reborn[0] = array('Strength' => 30, 'Intellect' => 30);
	$reborn[1] = array('Strength' => 40, 'Intellect' => 40);
	$reborn[2] = array('Strength' => 50, 'Intellect' => 50);
	$reborn[3] = array('Strength' => 60, 'Intellect' => 60);
	$reborn[4] = array('Strength' => 70, 'Intellect' => 70);
	$reborn[5] = array('Strength' => 80, 'Intellect' => 80);
	$reborn[6] = array('Strength' => 90, 'Intellect' => 90);

	// test if the current level should cause character to be reborn
	if(isset($reborn[$Reborns]) && $CurLevel == 120)
	{
		// update the _Char to the next level
		$db->dbQuery("UPDATE _Char set CurLevel = 1, MaxLevel = 1, Strength = {$reborn[$Reborns]['Strength']},
			Intellect = {$reborn[$Reborns]['Intellect']}, RemainSkillPoint = 0, ExpOffset = 0, RemainStatPoint = 0, Reborns = Reborns + 1
			WHERE CharID = '$CharID'");
		// delete existing _CharSkill data and populate with starting values
		$db->dbQuery("DELETE FROM _CharSkill WHERE CharID = '$CharID'");
		// you should actually have an array that defines the data to use in the following
		// and then either make one multi-value insert query or if that is not available,
		// use a loop to loop over the array of data
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 1, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 2, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 40, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 70, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8419, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8420, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 8421, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9354, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9355, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 9944, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 10625, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11162, 1)");
		$db->dbQuery("INSERT INTO _CharSkill (CharID, SkillID, Enable) VALUES ('$CharID', 11526, 1)");
		$db->dbQuery("UPDATE _CharSkillmMstery set Level = 0 WHERE CharID = '$CharID'");
	}
	elseif($Reborns == 7)
	{
		echo "Du hast bereits 7 Reborns ! ";
	}
	if ( $CurLevel !== 120)
	{
		echo 'Du bist nicht level 120 ! ';
	}
}else{
	echo "Das ist nicht dein Character!";
}
} else {
echo '<div class="page-body newsblock">';
echo '<div class="news-title">';
echo 'Reborn ur Char:';
echo '</div>';
echo '<div class="news-content">';
echo '<form method="post">';
echo 'Charactername:<br />';
// you should output a select menu or radio buttons that let the user select which character he wants
echo '<input onfocus="clearText(this);" style="background: rgba(0,0,0,0.5);width:220px;border-radius: 6px 6px 6px 6px;" type="text" maxlength="16" name="charname" value="charactername" /><br />';
echo '<br />';
echo '<input style="height:32px;vertical-align:middle;" class="button" type="submit" value="Submit" name="submitchange" />';
echo '</form>';
echo '</div>';
echo '</div>';
}
?>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.