不将数据插入数据库表

时间:2016-12-08 15:17:53

标签: php mysql pdo

我正在尝试将一些数据插入到我的testimage表中;但是,它没有插入它。它不会返回任何错误,我只知道它没有插入,因为我的insert语句有一个if / else语句。我知道它连接到我的数据库,因为我做了一个try / catch语句。

这是我的代码:

<?php

  require_once "Connection.php";
  $dbconnection = new Database('localhost', 'root', '', 'cardimages');

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

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

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

  if ($CardExec) {
    Echo "The data was inserted!";
  } else {
    echo "The data wasn't inserted!";
  }

  $query_array = array('name'=>$_POST['cardname'], 'type'=> 'card', '.jpg');
  $url = "http://gatherer.wizards.com/Handlers/Image.ashx?".http_build_query($query_array);
  $img = "Images/" . ucwords($Name) . ".png";
  file_put_contents($img, file_get_contents($url));

?>

这是我的Connection.php文件:

<?php

  Class Database {

    private $hostname;
    private $username;
    private $password;
    private $database;
    public $Connection;

    public function __construct($hostname, $username, $password, $database) {

        $this->hostname;
        $this->username;
        $this->password;
        $this->database;
        $this->Connect();

    }

    private function SetHostname($hostname) {

      $this->hostname = $hostname;

      return $this;

    }

    private function SetUsername($username) {

      $this->username = $username;

      return $this;

    }
    private function SetPassword($password) {

      $this->password = $password;

      return $this;

    }
    private function SetDatabase($database) {

      $this->database = $database;

      return $this;

    }

    public function GetHostname()  {

      return $this->hostname;

    }

    public function GetUsername()  {

      return $this->username;

    }

    public function GetPassword()  {

      return $this->password;

    }

    public function GetDatabase()  {

      return $this->database;

    }

    private function Connect() {

      try
       {
         $db = $this->Connection = new PDO('mysql:host=' . $this->hostname . ';dbname=' . $this->database . '', $this->username, $this->password);

         echo "This connected successfully!";

       }
       catch (PDOException $e)
       {

         echo "It didn't connect to the database!";

       }

        }

      }

    ?>

1 个答案:

答案 0 :(得分:2)

Database :: __构造中的前4行是做什么的?

提示:files[0]()$this->hostname=$hostname;

目前可能(但我不这么认为)连接有效, 但$this->SetHostname($hostname)永远不会被选中。