尝试插入时DBConnect :: prepare()错误

时间:2016-12-02 15:46:20

标签: php mysql pdo

我正在搞乱PDO而我正在尝试将一些数据插入到我的表中;但是,我收到DBConnect::prepare()错误。

这是我的insertcard.php文件:

<?php

  require_once "connection.php";

  $Name = $_POST['cardname'];
  $Colour = $_POST['colour'];
  $Rarity = $_POST['rarity'];

  $CardQuery = "INSERT INTO `cards`(`Name`, `Colour`, `Rarity`) VALUES (`:Name`,`:Colour`,`:Rarity`)";

  $CardResult = $dbconnection->prepare($CardQuery);
  $CardExec = $CardResult->execute(array(":Name"=>$Name,":Colour"=>$Colour,":Rarity"=>$Rarity));

  if ($CardExec) {
    Echo "Data Inserted!";
  }

?>

我在连接到我的数据库的connection.php中的函数:

private function Connect() {

  try {
    $db = $this->Connection = new PDO('mysql:host=' . $this->hostname . ';dbname=' . $this->dbname . '', $this->username, $this->password);
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
    echo "<br>Wow, we done goofed.";
  }

  }

实例化:

$dbconnection = new DBConnect('localhost', 'root', '', 'cardtest');

有谁知道这是什么问题?如果需要,我将展示更多的connection.php文件。

0 个答案:

没有答案