Hello everyone.. this is my php code

<div class="content-1 col-sm-12 nopadding">
    <div class="list-header col-sm-1">Date</div>
    <div class="list-header col-sm-2">Product</div>
    <div class="list-header col-sm-2">Username</div>
    <div class="list-header col-sm-1">Quantity</div>
    <div class="list-header col-sm-1">Rate</div>
    <div class="list-header col-sm-1">Sale Amount</div>
    <div class="list-header col-sm-1">Payment</div>
    <div class="list-header col-sm-1">Balance</div>
    <div class="list-header col-sm-1">Remarks</div>
</div>
<?php 
    $sql = "SELECT * FROM sale_history order by date ASC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<div style='background: #e3e6e1;' class='col-sm-12 nopadding'><div style='padding-left: 5px !Important;' class='list-content col-sm-1 nopadding'>" .$row['date']."</div>"; 
            echo "<div class='list-content col-sm-2'>" .$row['product']."</div>"; 
            echo "<div class='list-content col-sm-2'><a href='edit_user.php'>" .$row['username']."</a></div>"; 
            echo "<div class='list-content col-sm-1'>" .$row['quantity']."</div>"; 
            echo "<div class='list-content col-sm-1'>" .$row['rate']."</div>"; 
            echo "<div class='list-content col-sm-1'>" .$row['sale_amount']."</div>";
            echo "<div class='list-content col-sm-1'>" .$row['payment']."</div>";
            echo "<div class='list-content col-sm-1'>".$row['balance']."</div>";
            echo "<div class='list-content col-sm-1'>" .$row['remark']."</div></div>";

        }           
    } 
    else {
        echo "0 results";
        }

?>

output of this code will be
111.PNG

i want to add a delete button in each row. which button will delete this row from database, also upate balance row in another table.

Please help me...

Recommended Answers

All 3 Replies

Member Avatar for diafol

Depends how you're going to delete. If you're doing this via Ajax - then you may want to include the row id in a data attribute:

<button data-id="<?=$row['id']?>">Delete</button>

As in...

<?php
    $sql = "SELECT * FROM sale_history order by date ASC";
    $result = $conn->query($sql);
    $output = '';
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $output .= <<<EOT
            <div style='background: #e3e6e1;' class='col-sm-12 nopadding'><div style='padding-left: 5px !Important;' 
            class='list-content col-sm-1 nopadding'>
                {$row['date']}
            </div>
            <div class='list-content col-sm-2'>
                {$row['product']}
            </div>
            <div class='list-content col-sm-2'>
                <a href='edit_user.php'>{$row['username']}</a>
            </div>
            <div class='list-content col-sm-1'>
                {$row['quantity']}
            </div>
            <div class='list-content col-sm-1'>
                {$row['rate']}
            </div>
            <div class='list-content col-sm-1'>
                {$row['sale_amount']}
            </div>
            <div class='list-content col-sm-1'>
                {$row['payment']}
            </div>
            <div class='list-content col-sm-1'>
                {$row['balance']}
            </div>
            <div class='list-content col-sm-1'>
                {$row['remark']}
            </div>
            <div class='list-content col-sm-1'>
                <button class='del-item' data-id='{$row['id']}'>Delete</button>
            </div>
EOT;

        }
    } else {
        $output .= "0 results";
    }
?>
//at the appropriate point in your page...

<?=$output?>

Have a js listener for 'del-item' class click event, which then gets the data-id value and passes it to your php / mysql routine via Ajax. You can get the whole "row" deleted on the screen on return of appropriate response.

I did it with php... but is it right way? im in confusion...

   <?php 
        $sql = "SELECT * FROM sale_history order by date ASC";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo "<div style='background: #e3e6e1;' class='col-sm-12 nopadding'><div style='padding-left: 5px !Important;' class='list-content col-sm-1 nopadding'>" .$row['date']."</div>"; 
                echo "<div class='list-content col-sm-2'>" .$row['product']."</div>"; 
                echo "<div class='list-content col-sm-2'><a href='edit_user.php'>" .$row['username']."</a></div>"; 
                echo "<div class='list-content col-sm-1'>" .$row['quantity']."</div>"; 
                echo "<div class='list-content col-sm-1'>" .$row['rate']."</div>"; 
                echo "<div class='list-content col-sm-1'>" .$row['sale_amount']."</div>";
                echo "<div class='list-content col-sm-1'>" .$row['payment']."</div>";
                echo "<div class='list-content col-sm-1'>".$row['balance']."</div>";
                echo "<div class='list-content col-sm-1'>" .$row['remark']."</div>";
                echo "<div class='list-content col-sm-1'>
                        <form name='delete-form' action='full_history3.php' method='post'>
                            <input type='hidden' name='ID' value='$row[ID]'/>
                            <input type='hidden' name='username' value='$row[username]'/>
                            <input type='hidden' name='sale_amount' value='$row[sale_amount]'/>
                            <input class='delete-btn' type='submit' name='submit' value='Delete' onclick='show_confirm()' />
                        </form>
                    </div></div>";                                        
            }           
        } 
        else {
            echo "0 results";
        }
    if(isset($_POST['submit'])) {
        $username = $_POST['username'];
        $id = $_POST['ID'];
        $balance = $_POST['sale_amount'];

        $sql = "DELETE FROM sale_history WHERE id=$id;";
        $sql .= "UPDATE user_balance SET balance=balance-$balance WHERE username='$username'";

        if ($conn->multi_query($sql) === TRUE) {
            echo "Record Deleted Sucessfully";
    ?>
    <script>
        alert('Record Deleted Sucessfully!');
        window.location.href='full_history.php';
    </script>
    <?php
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        }
    ?>
Member Avatar for diafol

You don't want the data processing in the same page/file as the display. Separate out you html and php as far as possible (place bulk of PHP above the DTD and just use simple echoes and the odd loop/if i the main body of the page) and use another file for form handling.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.