0
我有这样的代码在product.php:在index.php文件如何更新从数据库中的记录表中显示
class Product {
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;
public function __construct($db) {
$this->conn = $db;
}
public function readAll()
{
$stmt = $this->conn->prepare('SELECT id, name, description, price, CategoryID, created FROM products');
$stmt->execute();
echo "<form action=\"./objects/product.php\" method=\"post\"> <table class=\"highlight responsive-table\">
<thead>
<tr>
<th data-field=\"empty\"> </th>
<th data-field=\"name\">Name</th>
<th data-field=\"description\">Description</th>
<th data-field=\"price\">Price</th>
<th data-field=\"category\">Category</th>
<th data-field=\"action\">Action</th>
</tr>
</thead>";
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $result['id'];
$n = $result['name'];
$d = $result['description'];
$p = $result['price'];
$ca = $result['CategoryID'];
$c = $result['created'];
echo "<tbody>
<tr>
<td style=\"width:10%;\">
<input type=\"checkbox\" id=\"checkbox_".$id."\" name=\"checkbox[]\" value=".$id." />
<label for=\"checkbox_".$id."\"></label>
</td>
<td style=\"width:15%;\">" .$n. "</td>
<td style=\"width:30%;\">" . $d. "</td>
<td style=\"width:10%;\">" ."$".$p. "</td>
<td style=\"width:15%;\">" . $ca. "</td>
<td style=\"width:20%;\">
<a class=\"waves-effect waves-light btn modal-trigger\" href=\"#modal2\" id=\"edit_".$id."\" name=\"edit[]\"><i class=\"material-icons\">mode_edit</i></a>
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
</td>";
}
echo "<input type=\"submit\" value=\"Delete\" name=\"delete\" id=\"delete\"/>
<input type=\"submit\" value=\"update\" name=\"update\" id=\"update\"/>
</form>";
echo "</tbody> </table>";
}
public function deleteSelected($ids) {
$query = 'DELETE FROM products WHERE id=?';
$stmt = $this->conn->prepare($query);
if (is_array($ids)) {
foreach ($ids as $id)
$stmt->execute([$id]);
}
else {
$stmt->execute([$ids]);
}
}
public function update() {
$sql2= "UPDATE `products` SET `Name` = :name, `Description` = :description, `Price` = :price, `CategoryID` = :catid WHERE `products`.`ID` = :id";
$stmt = $this->conn->prepare($sql2);
$stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
$stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
$stmt->bindParam(':price', $_POST['price'], PDO::PARAM_STR);
$stmt->bindParam(':catid', $_POST['catid'], PDO::PARAM_INT);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->execute();
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['delete']) && !empty($_POST['checkbox'])) {
$checkboxArr = $_POST['checkbox'];
foreach($checkboxArr as $id)
{
$cat = new Product($conn);
$cat->deleteSelected($id);
}
}
if (isset($_POST['update']) && !empty($_POST['edit'])) {
$editArr = $_POST['edit'];
$cat = new Product($conn);
$cat->update();
}
}
更新按钮:
<button class="btn waves-effect waves-light" type="submit" name="update" style="float:left; margin: 5px;">
<i class="material-icons left">mode_edit</i>Update
</button>
我显示的内容使用readAll函数在index.php中的表中创建数据库。在第一列中,我有复选框。在页面上也有几个按钮,其中一个按钮应该打开对话窗口,以便使用“更新”功能编辑记录。我试图通过删除数据库中的记录来做同样的事情,但它不起作用。该代码有什么错误?
是的,存在与产品的表,对于每个产品也有一个复选框和'edit'按钮,点击弹出窗口时,存在对一种产品和'Update'按钮的形式。所以,我删除了'foreach',我试图做到这一点,没有检查复选框,只需点击所选产品的编辑按钮,所以我添加到该按钮'id = \“edit _”。$ id。“\” name = \“edit [] \”',类似于复选框,但它仍然不起作用,并且我仍然不知道错误在哪里。我还修改了我所做的更改 – artur47wien