0

I need help with this code. My friend and I creating a website but we're stuck at:

Code is here:

<?php
        $servern = $_GET['server'];
        $vownerid = $mybb->user['uid'];
        $i1 = mysql_query("SELECT * FROM serversNew WHERE servername ='$servern'");
        if(mysql_num_rows($i1) == 0) {
                return;
        } else {
                while($row = mysql_fetch_assoc($i1)) {
                        $servername = $row['servername'];
                        $votes = $row['votes'];
                        $port = $row['port'];
                        $ip = $row['ip'];
                        $status = $row['status'];
                        $owner = $row['owner'];
                        $ownerid = $row['ownerid'];
                        $loader = $row['loader'];
                        $desc = $row['desc'];
                }

                if(($ownerid != $vownerid) && ($vownerid != 1)) {
                        return;
                } else {

                }
        }
?>
<?php
        if(isset($_POST['update'])) {
                $servername1 = htmlspecialchars($_POST['servername']);
                $ip1 = htmlspecialchars($_POST['ip']);
                $port1 = $_POST['port'];
                $owner1 = htmlspecialchars($_POST['owner']);
                $loader1 = $_POST['loader'];
                $desc1 = $_POST['desc'];
                if($loader1 == "no") {
                        $loader1 = $loader;
                }
                if($loader1 == "custom") {
                        $loader1 = htmlspecialchars($_POST['jar']);
                }
                                if($loader1 == 1) {
                                        $loader1 = "";
                                } else if($loader1 == 2) {
                                        $loader1 = "";
                                } else if($loader1 == 3) {
                                        $loader1 = "";
                                } else if($loader1 == 4) {
                                        $loader1 = "";
                                } else if($loader1 == 5) {
                                        $loader1 = "";
                                }
                if($port1 == "no") {
                        $port1 = $port;
                }
                mysql_query("UPDATE serversNew SET ip='$ip1', loader='$loader1', port='$port1', owner='$owner1', desc='$desc1' WHERE servername='$servername'");
                ?><script>window.location = "/member.php"</script><?php
        }
?>

We want to update the MYSQL query but it doesn't update it.

hope someone can help us.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • I think you should add your code to your question instead of linking to it. – Cyclonecode Jul 16 '13 at 09:23
  • do you get any errors? can't you reduce the amount of code needed to trigger the current error? – STT LCU Jul 16 '13 at 09:25
  • do you get any errors? can't you reduce the amount of code needed to trigger the current error? - No I don't have errors – user2586584 Jul 16 '13 at 09:26
  • if you are just getting started, please read: http://stackoverflow.com/q/12859942/2536029 – mnagel Jul 16 '13 at 09:26
  • Please don't use 'mysql' extension, it is deprecated, use instead Mysqli or PDO. – GAURAV MAHALE Jul 16 '13 at 09:27
  • Although it doesn't explain why your update doesn't work you should note that the mysql_query function is deprecated. You should use mysqli. – Roebie Jul 16 '13 at 09:27
  • http://pastebin.com/P7YXheK4 < this is my html file – user2586584 Jul 16 '13 at 09:29
  • Replace `mysql_query("UPDATE serversNew SET ip='$ip1', loader='$loader1', port='$port1', owner='$owner1', desc='$desc1' WHERE servername='$servername'");` by `echo("UPDATE serversNew SET ip='$ip1', loader='$loader1', port='$port1', owner='$owner1', desc='$desc1' WHERE servername='$servername'");`. Try the result directly in your database and also post it here. – Roebie Jul 16 '13 at 09:32
  • Also echo the return of mysqli_error after the update. See more on http://www.php.net/manual/en/mysqli.error.php – Roebie Jul 16 '13 at 09:36
  • `desc` is a reserved keyword, put it in backticks. – Petr R. Jul 16 '13 at 09:36
  • Good find Petr R.! You can also rename the field to eg "descr". – Roebie Jul 16 '13 at 09:37
  • @Roebie It doesn't work – user2586584 Jul 16 '13 at 09:37
  • What exactly doesn't work? – Roebie Jul 16 '13 at 09:38
  • by replacing: if I post my details then It shows on the next page the echo of ip loader port owner desc. – user2586584 Jul 16 '13 at 09:40
  • The replace should result in an update statement being echoed to the page. Let us see that statement and also test that statement directly in your database. Also test your update with the change Petr R. suggested. – Roebie Jul 16 '13 at 09:42
  • It works! with the backticks, Thanks! – user2586584 Jul 16 '13 at 09:46

2 Answers2

2

In your SQL query, put all these reserved words into backticks:

mysql_query("UPDATE serversNew SET ip='$ip1', loader='$loader1', port='$port1', owner='$owner1', `desc`='$desc1' WHERE servername='$servername'");

See also:

Community
  • 1
  • 1
Petr R.
  • 1,247
  • 2
  • 22
  • 30
0

No worries. Here i posted some customized classes for DB operations .

Check this.


**Config.php**

<?php    

if(!isset($_SERVER['DOCUMENT_ROOT'])){

if(isset($_SERVER['SCRIPT_FILENAME'])){

 $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',

 substr($_SERVER['SCRIPT_FILENAME'], 0,

  0-strlen($_SERVER['PHP_SELF']))); }; };

if(!isset($_SERVER['DOCUMENT_ROOT'])){

if(isset($_SERVER['PATH_TRANSLATED'])){

 $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',

substr(str_replace('\\\\', '\\', 

$_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));

}; };

  # Define DB  local


 define('DBHOST','localhost'); // host name//192.168.1.20

 define('DBUSER','xxxxxx'); // mysql user name 

 define('DBPASS','xxxxxxx'); // mysql pasword//142536

 define('DBNAME','xxxxx'); // databse name 


define('SITE_NAME','www.xxxxxx.co.in');


?>




Save this as  config.php


Next For DB operations Like Insert, Update, Select, Delete


**db_class.inc**

> <?php



class db_mysql
{
    var $database;
    var $hostname;
    var $username;
    var $password;
    var $link;

    function db_mysql($hostname, $username, $password, $database)
    {
        $this->hostname = $hostname;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
    }

    function connect()
    {   
        if(!$this->link = @mysql_connect($this->hostname,$this->username,$this->password) )
        {
            echo "Cannot Connect to host: ".$this->hostname;
            exit;
            return false;
        }
        else
        {
            return true;
        }
    }

    function select_db()
    {
        if(!@mysql_select_db($this->database, $this->link) )
        {
            echo "Cannot select database: ".$this->database;
            exit;
            return false;
        }
        else
        {
            return true;
        }
    }


    function query($query)
    {
        $result = @mysql_query($query, $this->link) or die($query.'--<font color=red>'.mysql_error().'</font>');
        return $result;
    }

    function fetch_one_row($query)
    {
        $result = @mysql_query($query, $this->link) or die($query.'--<font color=red>'.mysql_error().'</font>');
        $array = @mysql_fetch_row($result);
        if(is_array($array)) {
        foreach($array as $key => $val)
           $fetch_array[$key] = stripslashes($val);
         }  
        return $fetch_array;
    }


    function fetch_array($result)
    {
        $array = @mysql_fetch_array($result);
        if(is_array($array)) {
        foreach($array as $key => $val)
           $fetch_array[$key] = stripslashes($val);
         }  
        return $fetch_array;
    }

    function fetch_row($result)
    {
        $array = @mysql_fetch_row($result);
        if(is_array($array)) {
        foreach($array as $key => $val)
           $fetch_array[$key] = stripslashes($val);
         }  
        return $fetch_array;
    }

    function fetch_assoc($result)
    {
        $array = mysql_fetch_assoc($result);
        if(is_array($array)) {
        foreach($array as $key => $val)
           $fetch_array[$key] = stripslashes($val);
         }  
        return $fetch_array;
    }


    function delete_id($id, $field, $table)
    {
        if( is_array($id) )
        {
            foreach( $id AS $i )
            {
                $this->query("DELETE FROM $table WHERE $field = '$i'");
            }
        }
        else
        {
            $this->query("DELETE FROM $table WHERE $field = '$id'");
        }
    }

    function delete_sel_value($table,$field,$value)
    {
        $result=$this->query("SELECT $field FROM $table WHERE $field='$value'");
        $count = $this->fetch_row($result);

            return $count;
    }



    function num_rows($result)
    {
        return @mysql_num_rows($result);
    }

    function affected_rows()
    {
        return @mysql_affected_rows();
    }

    function insert_id()
    {
        return @mysql_insert_id();
    }

    function value_exists($array, $table)
    {
        /*array contains values of the field name and field value*/
        foreach($array as $key => $value)
        {
            $condition .= $key." = '".$value."' and ";
        }
        $condition = substr($condition,0,-5);
        $result = $this->query("SELECT COUNT(*) as cnt FROM ".$table." WHERE ".$condition);
        $count = $this->fetch_row($result);
        return $count[0];
    }

    function get_count($table,$condition='')
    {
        if($condition) $condition = " WHERE " . $condition;
        $result = $this->query("SELECT COUNT(*) as cnt FROM ".$table . $condition);
        $count = $this->fetch_row($result);
        return $count[0];
    }


    function update_from_array($array, $table, $field, $id)
    {
        $query = "UPDATE $table SET ";
        while(@list($key,$value) = @each($array))
        {
        if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false))
            {
                $fields[] = "$key='$value'";
            }
            else
            {
                $fields[] = "$key=$value";
            }
        }
        $query .= implode(', ', $fields);
        $query .= " WHERE ".$field." = '$id'";
        $this->query($query);
        return true;
    }


    function insert_from_array($array, $table)
    {
        while( @list($key,$value) = @each($array) )
        {
            $field_names[] = "$key";
            if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false) )
            $field_values[] = "'$value'";
            else
            $field_values[] = "$value";
        }
        $query = "INSERT INTO $table (";
        $query .= implode(', ', $field_names);
        $query .= ') VALUES (' . implode(',', $field_values) . ')';
        $this->query($query);
        return true;
    }
}  
// END CLASS
 ?>



//Read by GET method, if URL like  WWW.x.com/index.php?Get_par=req_Operation&count=1


//Use can use post method for getting **form** values 






>  if(function_exists($_GET['Get_par'])) {  
> 
> $_GET['Get_par']();
> 
> 
> }





//if get parameter value becomes **req_Operation**






> function req_Operation() {

>      require('config.php');

>        require('db_class.inc.php');

>        $db = new db_mysql(DBHOST,DBUSER,DBPASS,DBNAME);

>        $db->connect();

>        $db->select_db();
>   
>      $var1 =$_POST['encrypt1'];
>    
>      $var2 =$_POST['encrypt2'];
> 
> 
>       $query = "SELECT  X  FROM table_details where
> trim(table_key)='$var1'";
>   
>      $result = mysql_query($query) or die("Couldn't execute query.because: 
> 
> ".mysql_error());
> 
> 
> }
> 
> ?>
sathya
  • 25
  • 2