Witam,
chciałbym prosić o pomoc, robię projekt, w index.php wyświetlam zawartość bazy danych w tabeli przy użyciu funkcji readAll. W pierwszej kolumnie są checkboxy, których używam do usuwania rekordów z bazy funkcją deleteSelected, w ostatniej kolumnie dla każdego wyświetlonego produktu znajduje się przycisk, który otwiera okno dialogowe z formularzem do edycji wpisu i przyciskiem "update". Próbowałem zrobić to w podobny sposób jak z usuwanie, z tą tylko różnicą, że bez zaznaczania checkboxów, bo edytować można tylko jeden wpis jednocześnie, ale niestety nie działa. Czy moglibyście zerknąć na ten kod i wskazać co jest w nim źle?
Pozdrawiam
Zawartość product.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();
}
}
przycisk do usuwania:
<button class="btn waves-effect waves-light" type="submit" name="update">
<i class="material-icons left">mode_edit</i>Update
</button>