Jump to content

function problems


crazygol4

Recommended Posts

I've got a function that sanitize an array before it goes into a MySQL DB.  The code isn't mine, so naturally when it bricks I'm kind of confused on how to fix it.  I get the error : Fatal Error : Call to a member function on a non-object in ..... on line 33.

 

Here is where the original function is called:

$sql2 = insert("f_name, l_name, c_name, email, address, city, state, zipcode, country, phone, cell, fax, acctype, loginacc, accpass, comments, pending", array($_POST[f_name], $_POST[l_name], $_POST[c_name], $_POST[email], $_POST[address], $_POST[city], $_POST[state], $_POST[zipcode], $_POST[country], $_POST[phone], $_POST[cell], $_POST[fax], $_POST[acctype], $_POST[loginacc], md5('$_POST[accpass]'), $_POST[comments], $pending));

 

Here is are insert and sanitize functions:

function sanitize($var, $quotes = true) {
        if (is_array($var)) {   //run each array item through this function (by reference)       
            foreach ($var as $val) {
                $val = $this->sanitize($val);
            }
        }
        else if (is_string($var)) { //clean strings
            $var = mysql_real_escape_string($var);
            if ($quotes) {
                $var = "'". $var ."'";
            }
        }
        else if (is_null($var)) {   //convert null variables to SQL NULL
            $var = "NULL";
        }
        else if (is_bool($var)) {   //convert boolean variables to binary boolean
            $var = ($var) ? 1 : 0;
        }
        return $var;
    }

// MYSQL INSERTION FUNCTION
    //insert values into an database
    //  $rows = column names
    //  $vals = ARRAY of values;
    //  pass a 3rd arg as `true` to prevent sanitization
    //      $db->insert("name, email", array($name, $email))
    function insert($rows, $vals, $safe = false) {
        $vals = ($safe) ? $vals : $this->sanitize($vals);
        $this->sql = "INSERT INTO {$this->table} ( $rows ) VALUES (";
        foreach ($vals as $key => $val) {
            $this->sql .= ($key > 0) ? ", $val" : "$val";
        }
        $this->sql .= ")";
        return $this->query();
    }

 

Not exactly sure I'm even calling it right, so I would definitely appreciate any help!  BUT If you're going to tell me that I shouldn't be using code that I don't completely understand, save your breath....this is how I learn.

 

BTW, this is line 33 that bricks:

$vals = ($safe) ? $vals : $this->sanitize($vals);

 

I dont understand the $a ? $b : $c->function part, and there's not a way to find that just by the equation on a search engine, so if someone could explain what that lines is doing before it calls the function I would appreciate it greatly!

 

Thanks for your time and support!

-Mark

Link to comment
https://forums.phpfreaks.com/topic/133724-function-problems/
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.