maxelcat
maxelcat

Reputation: 1333

escape-data mysqli function wont work

I am trying to move from mysql to mysqli. I am connecting fine and simple queries seem to work. However, I want an escape_date function and it keeps tell me that

Notice: Undefined variable: conn in $data = mysqli_real_escape_string($conn, $data);

here's my code. The function is called when the form data is being processed.

ini_set('display_errors',1); 
error_reporting(E_ALL);

// connect to the database
$servername = "localhost";
$username = xxx;
$password = xxx;
$dbname = xxx;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo '<p>you are connected</p>';    
}


// escaping data function
function escape_data($data){
    //  address magic quotes
    if (ini_get('magic_quotes_gpc')){
        $data = stripslashes($data);
    }   
    $data = mysqli_real_escape_string($conn, $data);
    //return the escaped value
    return $data;
}

Upvotes: 0

Views: 187

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You got $conn nowhere within scope of escape_data(). Pass it as argument

Upvotes: 1

Related Questions