-2

I use this function to insert to database the user's errors. My public function is this:

public function set_error($var) {
        $dsn = 'mysql:***;host=***';
        $username = '***';
        $passd = '***';

        try {
            $pdo = new PDO($dsn, $username, $passd);
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
            $stmt = $pdo->prepare("INSERT INTO errors (ip, code, browser, referer, script, email, date) VALUES (:ip, :code, :browser, :referer, :script, :email, :date)");
            $params = array('ip' => $_SESSION['info_utente']['ip'], 'code' => $var, 'browser' => $_SESSION['info_utente']['browser'], 'referer' => $_SESSION['referer'], 'script' => $_SESSION['info_utente']['script'], 'email' => $_SESSION['sessione_attiva']['email'],'date' => $this->ftime());
            $stmt->execute($params);
            return TRUE;
        } catch (PDOException $e) {
            return FALSE;
        }
    }

I have a similar function (insert to account) and this works!

So i call the function set_error into another function (signup or login) if the user has an error, i call the function so: $this->set_error("error_1"); but doesn't work.

I checked the database and it is all ok! The name of columns are ok, and all are VARCHAR(255).

Precisely:

$this->set_error("error_1");
return "error_1";

Why this doesn't work?

Thanks in advance and sorry if my English is bad!

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • What's the error message in the exception $e? That should give you some indication of why it failed. But you're ignoring it. – Bill Karwin Oct 05 '13 at 16:37
  • Note: Before asking for help on what is wrong, remove `catch $e - Ignore` – webnoob Oct 05 '13 at 16:38
  • 1
    -1 for `return FALSE` alone, yet there are a dozen other errors in the code. – Your Common Sense Oct 05 '13 at 16:39
  • `date` is a reserved word in mySQL, that's likely to be breakign your query. See [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) for how to get an error message – Pekka Oct 05 '13 at 16:41
  • 2
    date is not a reserved word. – Mihai Oct 05 '13 at 16:44
  • In your array use ':ip'.... – Mihai Oct 05 '13 at 16:46
  • @Mihai It actually should work with or without the colons. – Michael Berkowski Oct 05 '13 at 16:46
  • @MichaelBerkowski the Prepare takes care of it even if would be a reserved word? – Mihai Oct 05 '13 at 16:48
  • 1
    @Mihai, parameter names have nothing to do with reserved words. Since MySQL doesn't support named parameters, PDO has to rewrite the query anyway to use positional parameters (`?`) before MySQL ever sees it. But MichaelBerkowski's comment was that the array of parameters sent to execute() don't need to be prepended with colon in current version of PHP. (Older PHP did require it.) – Bill Karwin Oct 05 '13 at 16:54

1 Answers1

-3

First, you should connect not in the method, nor in the application class. but you have create $pdo object somewhere and then pass it to application class in constructor and assign to a class variable.
Second, you have to configure PDO to actually throw errors. As well as you have to set up whole PHP error reporting.
Third, you have to get rid of this try..catch until you get an idea what is it for.

So, the function should be like

public function set_error($var) {
    $sql = "INSERT INTO errors (ip, code, browser, referer, script, email, date) 
                   VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $this->pdo->prepare($sql);
    $params = array($_SESSION['info_utente']['ip'],
                    $var, 
                    $_SESSION['info_utente']['browser'], 
                    $_SESSION['referer'], 
                    $_SESSION['info_utente']['script'],
                    $_SESSION['sessione_attiva']['email'],
                    $this->ftime()
               );
    $stmt->execute($params);
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I found the problem. All params (email and etc.) was NULL but in DB it could not be NULL. However thanks for the support and all suggestions. – Vincenzo Raco Oct 05 '13 at 17:45