Can't execute php to delete multiple rows in database

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:
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();
?>
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
A website communicates with PHP via JavaScript to tell PHP what to delete. Where is that?
 
Joined
May 12, 2023
Messages
3
Reaction score
0
A website communicates with PHP via JavaScript to tell PHP what to delete. Where is that?
sorry, im new to this programming. so what youre telling is that i need a js script that deletes data from database? because i have a page where you insert data into database with plain php. i thought i can do same with deletion. i must be wrong.
so what do i need to create this delete function? im just trying to follow tutorials on youtube and some forums. thank you in advance
 
Joined
Sep 4, 2022
Messages
131
Reaction score
16
hello !

<?php
print_r( $_POST ) ; will show you where are the datas through the POST global server var.
?>

you are using a wrong name to invoke the datas. it's not $_POST['id']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,920
Messages
2,570,038
Members
46,449
Latest member
onedumbsquirrel

Latest Threads

Top