I'm trying to make some functions to save easily my send-data. This why I'm trying to build it with functions but when I send information to the function I get errors:
Undefined variable: db in .... on line 53
Fatal error: Call to a member function prepare() on null in .... on line 53
My Question is, is it possible to save in a function a mysql statement?
PHP Code:
require_once("../connection.php");
function saveNews($k_id, $k_news)
{
$query_s_news = $db->prepare("UPDATE `benachrichtigung` SET `Newsletter` = :news WHERE `KundeID` = :id"); // This is the Errorline
$query_s_news->execute(array(
'news' => $k_news,
'id' => $k_id
));
}
if(isset($_POST["s_prod"]))
{
$s_news = filter_var($_POST["s_news"],
FILTER_SANITIZE_NUMBER_INT);
if($s_news)
{
if($s_news == 1)
{
saveNews($_SESSION["id"], 0);
}
else if($s_news == 2)
{
saveNews($_SESSION["id"], 1);
}
}
}
Is it possible?
//Edit: my full code and how I try to save it...