[PHP]Change Character function.

Results 1 to 16 of 16
  1. #1
    Account Upgraded | Title Enabled! Rejain is offline
    MemberRank
    Jul 2012 Join Date
    201Posts

    wink [PHP]Change Character function.

    Hey, i am trying to code a simple change character function, to learn a little about php and SQL combined with it.

    Now, i coded it but i dont get whats wrong heres my code :
    PHP Code:
    <?php
    session_start
    ();

    class 
    config
    {
    private 
    $DB 'GunzDB';
    private 
    $serv 'GUNZ/SQLEXPRESS';
    private 
    $username 'sa';
    private 
    $password 'Azzakl123';

    public function 
    __construct()
    {
    $con mssql_connect($this->serv$this->username$this->password) or die("<center><font color='red'>Failed to connect to the database!</font></center>");
        
    mssql_select_db($this->DB$con) or die("<center><font color='red'>Failed to select the database!</font></center>");
    }
    }
    $oldcharname $_POST['oldname'];
    $newcharname $_POST['newname'];

    function 
    alert($msg){
    echo 
    '<script language="javascript">alert("'.$msg.'");</script>';
    }

    if(isset(
    $_POST['changename']))
    {
        
    $q mssql_query("SELECT * FROM Character WHERE Name = '".$oldname."' AND SET Name = '".$newname."'");
        
    $row mssql_fetch_row($q);
        if(
    $row mssql_fetch_row($q) == 1)
        {
            echo 
    'Character '.$oldcharname.' has been changed to '.$newcharname.' .';
        }else{
            echo 
    'Please Enter BOTH Fields !';
        }
    }

    ?>
    the error is : Fatal error: Call to undefined function mssql_query() in C:\AppServ\www\testpanel\main.php on line 26

    and i have no idea how to fix, and yes abviously its the query problem but what in there is wrong ?! i dont get it, thanks anyway


  2. #2
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [PHP]Change Character function.

    It means that the php_mssql.dll extension is not loaded in PHP. Take a look at your php.ini file.

  3. #3
    Account Upgraded | Title Enabled! Rejain is offline
    MemberRank
    Jul 2012 Join Date
    201Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by SuperWaffle View Post
    It means that the php_mssql.dll extension is not loaded in PHP. Take a look at your php.ini file.
    I have done that, it wokred, now i have these errors :
    PHP Code:
    Warningmssql_query() [function.mssql-query]: Unable to connect to server: (nullin C:\AppServ\www\testpanel\main.php on line 26

    Warning
    mssql_query() [function.mssql-query]: A link to the server could not be established in C:\AppServ\www\testpanel\main.php on line 26

    Warning
    mssql_fetch_row(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\testpanel\main.php on line 27

    Warning
    mssql_fetch_row(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\testpanel\main.php on line 28
    Please Enter BOTH Fields 

    BTW how is the rest of the code, is it alright ? like i mean will it work ? thanks.

  4. #4
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [PHP]Change Character function.

    Uh not really, if you want my honest opinion: it sucks.
    But hey, we all have to start somewhere.

    I recommend you to write procedural code first before working with classes. (Because obviously, you don't really understand the purpose of it yet.)

    Also, the queries are not correct and it's vulnerable because you're using the raw POST values in the query.

  5. #5
    In Progress... FFXIV... Anju is offline
    MemberRank
    Oct 2010 Join Date
    Mist Ward 7 #38Location
    1,946Posts

    Re: [PHP]Change Character function.

    Correct me if I'm wrong, but it looks like your MSSQL connect is incorrect

  6. #6

    Re: [PHP]Change Character function.

    $serv should be 'GUNZ\SQLEXPRESS' (note the backslash).

    The class code looks terrible, you could honestly create a function handler for errors, or even better: don't use classes yet.

  7. #7
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: [PHP]Change Character function.

    You should probably change the query into two, or even three. it would be easier.
    Plus, if this is going to be for your users, what is going to check if the name is already used?
    You're variables in the query are not the ones you defined for the user input. (There is no $oldname defined)
    Also, you're using the mssql_fetch_row wrong. Use mssql_num_rows.
    One more, only select the columns you need.

    Here is what you could use. You should still sanitize the user input with a anti-sql function. It would become something like $oldname = anti_injection($_POST['oldname']); once you create one.
    PHP Code:
    <?php
    if(isset($_POST['changename']))
    {
    $oldname $_POST['oldname'];
    $newname $_POST['newname'];
        
    $query mssql_query("SELECT Name FROM Character WHERE Name = '".$oldname."'");
        
    $query2 mssql_query("SELECT Name FROM Character WHERE Name = '".$newname."'");
        
    // If both fields are set.
        
    if($oldname&&$newname)
        {
           
    // If new character name is already being used.
           
    if(mssql_num_rows($query2) > 0)
           {
              echo 
    "Character name is already in use!";
           }
           
    // Else name isn't being used.
           
    else
           {
              
    // Check if the character name exists.
              
    if(mssql_num_rows($query) > 0)
              {
                 
    $change mssql_query("UPDATE Character SET Name = '$newname' WHERE Name = '$oldname'");
                 echo 
    "Character name changed!";
              }
              
    // Else if the name doesn't exist.
              
    else
              {
                 echo 
    "Character name doesn't exist!";
              }
            }
        }
        
    // Else if both fields aren't set.
        
    else
        {
           echo 
    "Please enter both fields!";
        }
    }
    ?>
    Last edited by wesman2232; 01-02-13 at 09:30 AM.

  8. #8
    [R8]ℓσℓ32 caja is offline
    MemberRank
    Oct 2008 Join Date
    Here!Location
    1,502Posts

    Re: [PHP]Change Character function.

    I feel I'm the only who uses sqlsrvr.

    I've changed a few things, although I don't see this way the most optimized for this script.
    PHP Code:
        public function __construct() 
        { 
            
    $con mssql_connect($this->serv$this->username$this->password); 
            
    mssql_select_db($this->DB$con); 
        } 
        
        function 
    alert($msg)
        { 
            echo 
    '<script language="javascript">alert("'.$msg.'");</script>'
        } 
        
        
        if(isset(
    $_POST['changename']))
        {
            
    $oldname $_POST['oldname'];
            
    $newname $_POST['newname'];
            
    $query mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$oldname."'");
            
    $query2 mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$newname."'");
            
    // If both fields are set.
            
    if($oldname&&$newname)
            {
               
    // If new character name is already being used.
               
    if(mssql_num_rows($query2) > 0)
                  
    alert("Character name is already in use!");
               
    // Else name isn't being used.
               
    else
               {
                  
    // Check if the character name exists.
                  
    if(mssql_num_rows($query) > 0)
                  {
                     
    $change mssql_query("UPDATE [GunzDB].[dbo].[Character] SET [Name] = '$newname' WHERE [Name] = '$oldname'");
                     
    alert("Character name changed!");
                  }
                  
    // Else if the name doesn't exist.
                  
    else
                     
    alert("Character name doesn't exist!");
                }
            }
            
    // Else if both fields aren't set.
            
    else
               
    alert("Please enter both fields!");
        } 

  9. #9
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by caja View Post
    I feel I'm the only who uses sqlsrvr.

    I've changed a few things, although I don't see this way the most optimized for this script.
    PHP Code:
        public function __construct() 
        { 
            
    $con mssql_connect($this->serv$this->username$this->password); 
            
    mssql_select_db($this->DB$con); 
        } 
        
        function 
    alert($msg)
        { 
            echo 
    '<script language="javascript">alert("'.$msg.'");</script>'
        } 
        
        
        if(isset(
    $_POST['changename']))
        {
            
    $oldname $_POST['oldname'];
            
    $newname $_POST['newname'];
            
    $query mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$oldname."'");
            
    $query2 mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$newname."'");
            
    // If both fields are set.
            
    if($oldname&&$newname)
            {
               
    // If new character name is already being used.
               
    if(mssql_num_rows($query2) > 0)
                  
    alert("Character name is already in use!");
               
    // Else name isn't being used.
               
    else
               {
                  
    // Check if the character name exists.
                  
    if(mssql_num_rows($query) > 0)
                  {
                     
    $change mssql_query("UPDATE [GunzDB].[dbo].[Character] SET [Name] = '$newname' WHERE [Name] = '$oldname'");
                     
    alert("Character name changed!");
                  }
                  
    // Else if the name doesn't exist.
                  
    else
                     
    alert("Character name doesn't exist!");
                }
            }
            
    // Else if both fields aren't set.
            
    else
               
    alert("Please enter both fields!");
        } 
    Guess that makes 2 of us.
    Code of wesman2232 and caja looks fine, next to the SQLi.

  10. #10
    Account Upgraded | Title Enabled! Rejain is offline
    MemberRank
    Jul 2012 Join Date
    201Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by SuperWaffle View Post
    Uh not really, if you want my honest opinion: it sucks.
    But hey, we all have to start somewhere.

    I recommend you to write procedural code first before working with classes. (Because obviously, you don't really understand the purpose of it yet.)

    Also, the queries are not correct and it's vulnerable because you're using the raw POST values in the query.
    Your definitly right, cuz it took me a while to learn C++ and i did start somewhere, and about the classes isnt it, basiclly, the same as C++ ? and what is wrong with the POST function ? thank you very much.

    Quote Originally Posted by caja View Post
    I feel I'm the only who uses sqlsrvr.

    I've changed a few things, although I don't see this way the most optimized for this script.
    PHP Code:
        public function __construct() 
        { 
            
    $con mssql_connect($this->serv$this->username$this->password); 
            
    mssql_select_db($this->DB$con); 
        } 
        
        function 
    alert($msg)
        { 
            echo 
    '<script language="javascript">alert("'.$msg.'");</script>'
        } 
        
        
        if(isset(
    $_POST['changename']))
        {
            
    $oldname $_POST['oldname'];
            
    $newname $_POST['newname'];
            
    $query mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$oldname."'");
            
    $query2 mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$newname."'");
            
    // If both fields are set.
            
    if($oldname&&$newname)
            {
               
    // If new character name is already being used.
               
    if(mssql_num_rows($query2) > 0)
                  
    alert("Character name is already in use!");
               
    // Else name isn't being used.
               
    else
               {
                  
    // Check if the character name exists.
                  
    if(mssql_num_rows($query) > 0)
                  {
                     
    $change mssql_query("UPDATE [GunzDB].[dbo].[Character] SET [Name] = '$newname' WHERE [Name] = '$oldname'");
                     
    alert("Character name changed!");
                  }
                  
    // Else if the name doesn't exist.
                  
    else
                     
    alert("Character name doesn't exist!");
                }
            }
            
    // Else if both fields aren't set.
            
    else
               
    alert("Please enter both fields!");
        } 
    Thanks for your support


    Quote Originally Posted by Linear8
    $serv should be 'GUNZ\SQLEXPRESS' (note the backslash).

    The class code looks terrible, you could honestly create a function handler for errors, or even better: don't use classes yet.
    u should read what SupperWaffle said, i just started coding php a little while ago, dont be so rough on me.

    EDIT : Thank you guys very much, i finally managed to do it, and it works thanks
    Last edited by Rejain; 01-02-13 at 11:46 AM.

  11. #11
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by caja View Post
    I feel I'm the only who uses sqlsrvr.

    I've changed a few things, although I don't see this way the most optimized for this script.
    PHP Code:
        public function __construct() 
        { 
            
    $con mssql_connect($this->serv$this->username$this->password); 
            
    mssql_select_db($this->DB$con); 
        } 
        
        function 
    alert($msg)
        { 
            echo 
    '<script language="javascript">alert("'.$msg.'");</script>'
        } 
        
        
        if(isset(
    $_POST['changename']))
        {
            
    $oldname $_POST['oldname'];
            
    $newname $_POST['newname'];
            
    $query mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$oldname."'");
            
    $query2 mssql_query("SELECT [Name] FROM [GunzDB].[dbo].[Character] WHERE [Name] = '".$newname."'");
            
    // If both fields are set.
            
    if($oldname&&$newname)
            {
               
    // If new character name is already being used.
               
    if(mssql_num_rows($query2) > 0)
                  
    alert("Character name is already in use!");
               
    // Else name isn't being used.
               
    else
               {
                  
    // Check if the character name exists.
                  
    if(mssql_num_rows($query) > 0)
                  {
                     
    $change mssql_query("UPDATE [GunzDB].[dbo].[Character] SET [Name] = '$newname' WHERE [Name] = '$oldname'");
                     
    alert("Character name changed!");
                  }
                  
    // Else if the name doesn't exist.
                  
    else
                     
    alert("Character name doesn't exist!");
                }
            }
            
    // Else if both fields aren't set.
            
    else
               
    alert("Please enter both fields!");
        } 
    I'm going to do a project for a website like Emisand's Admin Panel where you can switch between using ODBC and MSSQL, but also include SQLSERV. I feel like I should know that since MSSQL is no longer being used in the latest PHP

  12. #12
    [R8]ℓσℓ32 caja is offline
    MemberRank
    Oct 2008 Join Date
    Here!Location
    1,502Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by Linear88 View Post
    $serv should be 'GUNZ\SQLEXPRESS' (note the backslash).

    The class code looks terrible, you could honestly create a function handler for errors, or even better: don't use classes yet.
    You shouldn't use SQL Server Express if you plan to run a server.

  13. #13
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: [PHP]Change Character function.

    Quote Originally Posted by caja View Post
    You shouldn't use SQL Server Express if you plan to run a server.
    Why do you say that? All GunZ servers that I've ever heard of use the SQL EXPRESS Database Engine.

  14. #14
    [R8]ℓσℓ32 caja is offline
    MemberRank
    Oct 2008 Join Date
    Here!Location
    1,502Posts

    Re: [PHP]Change Character function.

    The problem is the Express edition. You need the datacenter version for run the gunz server properly. The express edition is usseles.

  15. #15

    Re: [PHP]Change Character function.

    Quote Originally Posted by caja View Post
    The problem is the Express edition. You need the datacenter version for run the gunz server properly. The express edition is usseles.
    It actually runs fine under SQL Express. It's been mentioned in every guide since the release of the 2005 server files anyway, so...

  16. #16
    [R8]ℓσℓ32 caja is offline
    MemberRank
    Oct 2008 Join Date
    Here!Location
    1,502Posts

    Re: [PHP]Change Character function.

    Run SQL Profiler and look all issues is causing the express edition. The problem is that people just do whatever the guides say.

    Here you will find all limitations.
    Features Supported by the Editions of SQL Server 2008 R2



Advertisement