簡體   English   中英

不將數據插入數據庫表

[英]Not Inserting Data Into Database Table

我正在嘗試在testimage表中插入一些數據; 但是,它沒有插入。 它不會返回任何錯誤,我只知道它不會插入,因為我的插入語句有一個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!";

       }

        }

      }

    ?>

Database :: __ construct的前4行是什么?

提示: $this->hostname=$hostname; $this->SetHostname($hostname)

當前也許(但是我不這么認為)連接有效,但是從不選擇cardimages

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM