我有一个PHP类,我想创建我可以稍后从数据库中提取的数据更新的实例。这是我到目前为止所得到的:
<?php
$servername = "localhost";
$username = "super";
$password = "cala";
$database = "fraga";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$getTableQuery = "SELECT ani.Id, ani.Name, ani.Size, ani.Color, tbs.Name as Species, tbs.Description
FROM animals as ani INNER JOIN
animalTypes as tbs ON ani.Species = tbs.Id
ORDER BY ani.Id";
$table = $conn->query($getTableQuery);
$pageLoaded = false;
if(isset($_POST['btnInsert']) && ($_POST['txtName'] != "")){
$pageLoaded = true;
}
if ($table->num_rows > 0) {
echo "<table border='1'><tr><th>Name</th><th>Size</th><th>Color</th><th>Species</th></tr>";
// output data of each row
while($row = $table->fetch_assoc()) {
echo "<tr><td>".$row["Name"]."</td><td>".$row["Size"]."</td><td>".$row["Color"]."</td><td>".$row["Species"]."</td></tr>";
$fish[] = $row;
}
echo "</table>";
echo "</br>";
} else {
echo "0 results";
}
if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert") && $pageLoaded == true)
{
$Animal = new Animal($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txtSpecies'], $_POST['txtDescription']);
$Animal->InsertAnimal($conn);
}else if(isset($_POST['btnSave']) && ($_POST['btnSave'] == "Save") && $pageLoaded == true){
$Animal->UpdateAnimal($Animal);
}
class Animal
{
private $name = "Animal Name";
private $size = 0;
private $color = "255:255:255";
private $speciesName = "Species Name";
private $speciesDescription = "Species Description";
public function Animal($name, $size, $color, $species, $description){
$this->name = $name;
$this->size = $size;
$this->color = $color;
$this->speciesName = $species;
$this->speciesDescription = $description;
}
private function ColorCheck($color){
if($color >= 256 || $color <= 0)
return false;
else
return true;
}
public function InsertAnimal($conn, $pageLoaded){
$this->speciesName = mysqli_real_escape_string($conn, $this->speciesName);
$this->speciesDescription = mysqli_real_escape_string($conn, $this->speciesName);
$this->name = mysqli_real_escape_string($conn, $this->name);
$this->size = mysqli_real_escape_string($conn, $this->size);
$this->color = mysqli_real_escape_string($conn, $this->color);
$speciesId = "SELECT Id from animalTypes WHERE Name = '$this->speciesDescription'";
$speciesInsert = "INSERT IGNORE INTO animalTypes (Name, Description)
VALUES ('$this->speciesName', '$this->speciesDescription')";
$result = mysqli_query($conn, $speciesInsert) or die("Query fail: " . mysqli_error($conn));
if($id = $conn->query($speciesId)){
$row = $id->fetch_assoc();
$intId = $row['Id'];
}
$AnimalInsert = "INSERT INTO animals (Name, Size, Color, Species)
VALUES ('$this->name', $this->size, '$this->color', $intId)";
$result2 = mysqli_query($conn, $AnimalInsert) or die("Query fail: " . mysqli_error($conn));
echo '<script type="text/javascript">window.location = window.location.href;</script>';
$_POST['txtName'] = "";
}
public function UpdateAnimal($animal, $conn){
$speciesCheck = "SELECT * FROM animalTypes WHERE Name = '$this->speciesName";
$speciesList = mysqli_query($conn, $speciesCheck) or die("Query fail: " . mysqli_error($conn));
$updateQuery = "UPDATE animals";
}
}
$conn->close();
?>
<body>
<form action="index.php" method="post">
Animal Name:<br />
<input name="txtName" type="text" /><br />
<br />
Size:<br />
<input name="txtSize" type="text" /><br />
<br />
Color:<br />
<input name="txtColor" type="text" /><br />
<br />
Species Name:<br />
<input name="txtSpecies" type="text" /><br />
<br />
Species Description:<br />
<input name="txtDescription" style="width: 419px; height: 125px" type="text" /><br />
<br />
<input name="btnInsert" type="submit" value="Insert" />
<input name="btnSave" type="submit" value="Save" />
</form>
</body>
现在,我想要做的是从页面加载时加载的数据创建Animal
的实例,并存储它们以进行更新。问题是,我不知道该怎么做。我已经google了一下(但我的确认为很弱),并且看到了创建Animals
数组并在while
循环期间添加它们的建议。这真的是最好的方法吗?然后,我如何将实例加载回文本框,以便我可以更新它们?
答案 0 :(得分:1)
首先,您应该学会正确地分离代码中的问题。 如果我开始解释你应该如何从头开始构建你的脚本,这将花费太长时间,所以我会尽力给你一个好的方向。我认为这将有助于您在学习过程中获得更多。
所以,如果我理解正确,你发布的代码都设置在一个文件中,我想它在你的index.php里面? (在这里遗漏了一些信息)
如果是这种情况......
使用index.php显示db中的“Animals”列表,而不是更多。每个列表条目旁边都会有一个编辑和删除按钮/链接。在列表顶部放置一个名为 create 的链接。
现在你所有的index.php都是从db获取动物并列出它们。
将这部分代码放在另一个名为dbconfig.php
的文件中$servername = "localhost";
$username = "super";
$password = "cala";
$database = "fraga";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
在开始编写index.php脚本之前,将它包含在index.php之上。
include 'dbconfig.php'
现在您可以在index.php中使用$ conn变量。我们把它放到另一个文件中并包含它,因为我们将在接下来的步骤中重用这个部分。
我不会在这里为index.php编写你的全部代码,我想你会掌握它。也许您会问自己创建,编辑,删除链接应该做什么。
所以在我写“IDOfYourAnimalInsideDB”的地方你必须输出实际的id,这将在你的while循环中完成。
首先,在这里再次包含dbconfig.php,这样就可以使用具有db连接的$ conn变量。
检查请求是否包含一些发布变量,如果为true,则构建一个动物实例并将其写入db。
在之外构建表单。因此无论是否发布都没关系,您将显示创建表单。
再次首先包含dbconfig.php。 然后你要检查是否设置了$ _GET ['id'],如果它大于0并且它是一个整数值。如果是这样,请执行删除sql到db。
再次首先包含dbconfig.php。 然后,您想再次检查GET参数并构建一个sql请求以获取特定的数据库条目。 输出已包含db值的表单。如果有一个post请求,你创建一个新的Animal实例,用$ _POST中的数据填充它,然后用它来更新你的db。
动物类中的向您的动物类添加getter函数,以便您可以从外部访问私有属性。你应该写你在create.php,update.php,delete.php或你用于数据库操作的另一个类中创建,更新,删除逻辑。在那里你想要访问属性,例如为了建立你的更新sql。 因此,为“Animal”模型类的每个属性制作一个getter方法
public function getName() {
return $this->name;
}
所以从外面你可以得到你的动物名字
$animalName = $animal->getName();
如果你需要更具体的东西,你应该多指一点你的问题。我所描述的只是一种将脚本分成可以理解和维护的部分的方法,因为结构和正确的分离是编程中最重要的事情之一。 我所描述的远离干净的“CRUD”解决方案,但我认为这对您来说是一个小步骤,您现在可以采取更接近干净的解决方案。
亲切的问候