Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to create a delete button for each row I entered according to the ID I listed in my database.

What I have tried:

This is my current code :

<html>

<head>
    <link rel="stylesheet"  href="css/booklist.css">
    <title>Search data by its ID</title>

<script type="text/javascript">
function out(){
     alert("Checkout Items Successful!");
    }
</script>

</head>
<div class ="background">
    <h1>Library Checkout System</h1>
    <h2>Loan/Request Your Books Here</h2>

        <form action="" method="POST">
            <input type="text" name="id" placeholder="Please enter Book ID" />
            <input type="submit" name="search" value="Search By ID" />
        
        </form>
        <table border="2" id="newton">
            <tr>

                <th>Product Name</th>
                <th>Quantity</th>
                <th>Returned Date</th>
            </tr><br><br>

            <?php
            $connection = mysqli_connect("localhost","root", "");
            $db = mysqli_select_db($connection,"myfirstdb");
            session_start();

            if (!isset($_SESSION['id'])) {
                $_SESSION['id'] = array();
            }
            

            if(isset($_POST['search']))
            {
                $id = $_POST['id'];

                array_push($_SESSION['id'],$id); //Here we have declared a session array. We can add elements to it by array_push function.//
                $_SESSION['id'] = array_unique($_SESSION['id']); //array_unique: Removes duplicate values from an array


                $id = implode(',',$_SESSION['id']);  //implode : returns a strings from the elements of an array.//
         // instead of changing the database of each row, the implode() function adds the rows according to the selected id entered.//
         // this function is the key for solving my original problem.//    
               
               

                $query = "SELECT * FROM `table3` where id in ($id)";
                $query_run = mysqli_query($connection, $query);

                while($row = mysqli_fetch_array($query_run))
                {
                    ?>
                    <tr>
                        <td>
          <?php echo $row ['product_name']; ?> </td>
        <td>
          <?php echo $row ['quantity']; ?> </td>
        <td>
          <?php echo $row ['returned_date']; ?> </td>


                    </tr>

                    <?php

                }
            }


            ?>
        
        </table>
        <br>
        <input type="submit" name="send" value="Confirm Booklist" onclick="out()" />
        </form>
</div>
</body>
<a href=""></a><a href=""></a>
</html>
Posted
Updated 16-Nov-20 8:30am
Comments
Richard Deeming 16-Nov-20 3:06am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Richard Deeming 16-Nov-20 3:07am    
Beyond that, you haven't asked a question. You've told us what you're trying to do, and shown us the code you're using. But you haven't described the problem you have.

Click the green "Improve question" link and update your question to include a proper description of your problem. Include the full details of any errors, and remember to indicate which line of code they relate to.

1 solution

You will need the following to do what you describe:

An <input> of type='button' or a <button> element.
An onclick event for that button
An AJAX call for the onclick event
A PHP page on the server to accept data from you AJAX call (you can use the SESSION variables but I'd think you'd be better off with JSON.
A SQL statement in the php target of the AJAX call to do your delete
(via a loop or with an IN clause, depending upon what you think is best)

Optionally, redo your original data request (with the now reduced data set due to the deletions) and send it back to update the innerHTML of your table.

That's the "HOW" in terms of flow. Hopefully you are knowledgeable in these various parts of "web technology".

Notice: no <form> element - AJAX is much better, in generally (IMO).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900