PDO INSERT无效但没有错误

时间:2018-02-02 12:05:10

标签: php pdo

我制作了这个脚本来生成一个字符串并将其插入到数据库中, 但它并没有插入到数据库中,即使我没有得到任何错误。

$pare = $id;
$time_stamp = date('H:m');
$token = 'token'. md5($pare . $time_stamp);

echo " Token: -" . $token;
try {
  $database = $this->server->connect_to_database('3250900');
  $sql_query_string = "INSERT INTO `authentication_tokens` (`id`, `user_id`, `token`, `timestamp`) VALUES (:n, :user_id, :token, :time_stamp)";
  $statement = $database->prepare($sql_query_string); //Prepare the sql statement
  $statement->execute([ ':n'          => NULL,
                        ':user_id'    => $pare,
                        ':token'      => $token,
                        ':time_stamp' => $time_stamp]); //execute query
} catch (Exception $e) {
  echo $e;
}


print_r($statement);

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    $pare = $id;
    $time_stamp = date('H:m');
    $token = 'token'. md5($pare . $time_stamp);


 echo " Token: -" . $token;
 try {
    $database = $this->server->connect_to_database('3250900');

    $stmt = $database ->prepare ("INSERT INTO `authentication_tokens` (`id`, `user_id`, `token`, `timestamp`) VALUES (:n, :user_id, :token, :time_stamp)");
    $stmt -> bindParam(':n', NULL);
    $stmt -> bindParam(':user_id', $pare);
    $stmt -> bindParam(':token', $token);
    $stmt -> bindParam(':timestamp', $time_stamp);
    $result = $stmt -> execute();

    }
    catch (PDOException $e) {
           trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
        }

    if ($result) {
        return $stmt->rowCount();
    }