2016-12-08 6 views
0

私の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!"; 

     } 

     } 

     } 

    ?> 
+3

'Database :: __ construct' doの最初の4行は何ですか?ヒント: '$ this-> hostname = $ hostname;'または '$ this-> SetHostname($ hostname)'現在*多分*ですが、接続はうまくいきませんが、 'cardimages'は決して選択されません。 – JustOnUnderMillions

+0

実行後にエラーをチェックしなかったときにエラーを返さなかったことをどのように知っていますか? – aynber

+0

@JustOnUnderMillionsそれを修正しました!それを答えとして追加できますか? –

答えて

2

データベースの最初の4行:: __構築物がやるとは何ですか?

ヒント:$this->hostname=$hostname;または

$this->SetHostname($hostname)は現在、多分接続は、動作します しかしcardimagesが選択されることはありません(しかし、私は事はとてもいけません)。

+0

私はそのミスをしたとは信じられません。どうもありがとう! –

+0

新鮮な目が必要な場合もあります;-) – JustOnUnderMillions

関連する問題