I created a PHP class to update the data of a record in a table in the mysql database:
<?php
class InsertAccess{
public function insert_access($reg){
require_once('../conn/conecta.php');
$status = '1';
try{
$sql = "UPDATE 'tab_login' SET 'last_access' = :date_access, 'status' = :sts WHERE 'registro' = :reg";
$access = $conecta->prepare($sql);
$access->bindParam(":reg", $reg, PDO::PARAM_STR);
$access->bindParam(":date_access", date('Y-m-d H:i:s'), PDO::PARAM_STR);
$access->bindParam(":sts", $status, PDO::PARAM_STR);
$access->execute();
}catch (Exception $ex){
echo 'ERRO: '.$ex;
}
}
}
?>
You're giving me an undefined variable message:
Notice: Undefined variable: conecta in C:\wamp64\www\my_dir\actions\class_access.php on line 13
Can anyone tell me the correct way to use a php class to perform this type of action?
connecta.php
try {
$servidor = 'localhost';
$usuario = 'root';
$senha = '';
$banco = 'my_db';
$conecta = new PDO("mysql:host=$servidor;dbname=$banco", $usuario , $senha, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$conecta->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e) {
echo 'ERRO DE CONEXÃO: ' . $e->getMessage();
}
Function call
$access_class = new InsertAccess();
$access_class->insert_access($item);
The InsertAccess class is a separate php file, and I'm using it in another php file by doing the instance as shown above.