- Joined
- May 12, 2023
- Messages
- 3
- Reaction score
- 0
I'm trying to create a page where data is being displayed from database where data was inserted by other page. My data is displayed and i made checkboxes for it to select and a button to delete them. i can select them and when i press delete i made echo when it says that record deleted. Cant figure out why it doesnt actually delete from database.
My index:
My PHP code:
My index:
PHP:
<?php
session_start();
require_once 'connection.php';
$sql = "SELECT * FROM test";
$all_product = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="productList">
<div class="title">
<h1>Product List</h1>
</div>
<div class="button-container">
<a href="add-product.html">
<input type="button" class="addButton" value="ADD">
</a>
<input type="submit" form="list-check-box" name="delete" id="#delete-product-btn" value="MASS DELETE" method="post">
</div>
</div>
<hr class="divLine">
<div class="show-list">
<?php
while($row = mysqli_fetch_assoc($all_product)){
?>
<form class="grid-item" id="list-check-box" action="submit-copy.php" method="post">
<div class="list" >
<p class="sku">
<input type="checkbox" id=".delete-checkbox" class="delete_checkbox" name="delete-product-btn[]" value="<?php $row['id'];?>">
<?php echo $row["sku"];?></p>
<p class="name"><?php echo $row["name"];?></p>
<p class="price"><?php echo $row["price"];?></p>
</div>
<div class="size">
<p class="size"><?php echo $row["size"];?></p>
</div>
<div class="weight">
<p class="weight"><?php echo $row["weight"];?></p>
</div>
<div class="furniture">
<p class="height"><?php echo $row["height"];?></p>
<p class="width"><?php echo $row["width"];?></p>
<p class="length"><?php echo $row["length"];?></p>
</div>
</form>
<?php
}
?>
</div>
<div class="footer">
<hr class="divLine">
<h4>Scandiweb Test assignment</h4>
</div>
</body>
<?php
include_once 'submit.php';
?>
</html>
My PHP code:
Code:
<?php
session_start();
$id = $_POST["id"];
$host = "localhost";
$dbname = "productData";
$username = "root";
$password = "root";
$conn = mysqli_connect($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM test WHERE id IN($id)";
if(isset($_POST['delete'])){
$all_id = $_POST['delete-product-btn'];
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>