If you want your function works you have to pass as argument the $mysqli object
Like this:
function secure ($string,$mysqli) {
$string = htmlspecialchars($string);
$string = strip_tags($string);
$string = stripcslashes($string);
$string = $mysqli -> real_escape_string($string);
return $string;}
I advice to you, don't use this function:
1- Is over kill and wrong @Dagon's comment.(Agree)
2- Since you're using mysqli, why are you doing any of this, when you could be using prepared statements? @barmar's comment.(Agree)
3- You shouldn't use htmlspecialchars() when putting into the database, you should use htmlentities() when rendering @bamar comment.(Agree)
Above are for me the main reason for not using
You can use Prepare Staments instead,
an example from the manual
/* create a prepared statement */
$stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?");
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* close statement */
$stmt->close();