gpt4 book ai didi

php - 不将数据插入数据库表

转载 作者:行者123 更新时间:2023-11-29 07:07:39 25 4
gpt4 key购买 nike

我正在尝试将一些数据插入到我的 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 从未被选中。

关于php - 不将数据插入数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41042795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com