duanlu6114 2017-07-05 16:32
浏览 36

PDO编写的声明插入不起作用

I absolutely have no clues about why this prepared statement doesn't work returning false but without giving me any error. I've read similar questions here on StackOverflow but I didn't find any tip to solve this unknown problem.

The errors are activated

public function PDOconn() {
    try {
        return new PDO($this->getDsn(), $this->username, $this->password, [PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION]);
    } catch (PDOException $e) {
        echo "Errore di connessione al database";
        throw $e->getCode();
    }
}

This is the PHP file that handles the AJAX request. I inserted the query into a try {} ... catch() but it actually didn't help since it doesn't report any error. execute() just returns false

/* ***** CLASSES AUTOLOAD fn ***** */
spl_autoload_register(function ($class_name) {
    require_once '../classes/' . $class_name . '.php';
});

$db     = new database();
$conn   = $db->PDOconn();


$name   = postGet::post('name');
$email  = postGet::post('email');
$commercial = postGet::post('commercial');


try {
    $stmt = $conn->prepare('INSERT INTO newsletter_subscription(name, email, commercial) VALUES(:name, :email, :commercial)');
    $stmt->execute(array(':name' => $name, ':email' => $email, ':commercial' => $commercial));
} catch (PDOException $e) {
    $return = $e->getCode();
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($return);

The variables string $name, string $email and int $commercial are correctly passed, I printed them into the php file.

This is the table structure where subscriptorID is PRIMARY AI and email is UNIQUE enter image description here

[SOLUTION] Shame on me and on my short memory but maybe it can help someone else: I had the flash that that DB user didn't have INSERT priviledges, that's why the query returned just false without any error.

  • 写回答

2条回答 默认 最新

  • dream12001 2017-07-05 16:40
    关注

    bool $commercial is passed as integer and not boolean. Change it in your sql to boolean which is True or False

    评论

报告相同问题?