code shows no error but still does not run DELETE

Asked

Viewed 87 times

0

code shows no error but still does not run DELETE.

I created a tag to delete line by line and I used GET for this and it’s still not working.

tag <td><a href="delete_rows.php?del=$row[ISBN]">delete</a></td>

get method:

<?php
include_once('config.php');
if( isset($_GET['del']) )
{
    $ISBN = $_GET['del'];
    $sql= "DELETE FROM books WHERE ISBN='$ISBN'";
    $res= mysqli_query($conn,$sql) or die("Failed".mysqli_error($conn));
    echo "<meta http-equiv='refresh' content='0;url=update.php'>";
}    
?>

Edit to add the full code where the tag is

<html>
<head>
<title></title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>

        function updateBook(id)
        {    
            var isbn = document.getElementById("ISBN"+id).innerHTML;
            document.getElementById("ISBN").value = isbn;

             var title = document.getElementById("Title"+id).innerHTML;
            document.getElementById("Title").value = title;

             var authorsname = document.getElementById("Authorsname"+id).innerHTML;
            document.getElementById("Authorsname").value = authorsname;

             var edition = document.getElementById("edition"+id).innerHTML;
            document.getElementById("edition").value = edition;

             var year = document.getElementById("year"+id).innerHTML;
            document.getElementById("year").value = year;

             var category = document.getElementById("category"+id).innerHTML;
            document.getElementById("category").value = category;

             var publisher = document.getElementById("publisher"+id).innerHTML;
            document.getElementById("publisher").value = publisher;

             var quantityinstock = document.getElementById("quantityinstock"+id).innerHTML;
            document.getElementById("quantityinstock").value = quantityinstock;

             var price = document.getElementById("price"+id).innerHTML;
            document.getElementById("price").value = price;
        }

        $(document).ready(function(){
            $(".update").click(function(){
                $("#UPDT").toggle();
                $(this).text(function(i,text){
                    return text === "See less" ? "Update" : "See less";
                });
            });
        });
    </script>



</head>
    <body>
<?php
    include('config.php');
    $query = "SELECT * FROM books ORDER BY Title ASC";
    $r=mysqli_query($conn, $query);
?>
    <table cellpadding="2" cellspacing="2" border="0">
        <tr>
    <th>ISBN</th>
    <th> Title </th>
    <th> Author's name</th>
    <th> edition</th>
    <th> year</th>
    <th> category</th>
    <th> publisher</th>
    <th> quantity-in-stock</th>
    <th> price</th>
    </tr>
<?php $id = 0;?>
<?php while($books =mysqli_fetch_object($r)){?>
    <?php $id = $id +1;?>
    <tr>
    <td id="ISBN<?php echo $id ?>"> <?php echo $books->ISBN; ?></td>
    <td id="Title<?php echo $id?>"> <?php echo $books->Title;  ?></td>
    <td id="Authorsname<?php echo $id?>"> <?php echo $books->Authorsname; ?></td>
    <td id="edition<?php echo $id?>"> <?php echo $books->edition;?></td>
    <td id="year<?php echo $id?>"><?php echo $books->year;   ?></td>
    <td id="category<?php echo $id?>"> <?php echo $books->category;   ?></td>
    <td  id="publisher<?php echo $id?>"><?php echo $books->publisher;  ?></td>
    <td id="quantityinstock<?php echo $id?>"> <?php echo $books->quantityinstock; ?></td>
    <td id="price<?php echo $id?>"> <?php echo $books->price; ?></td>
    echo '<img src="data:image/jpeg;base64,'.base64_decode( $books->Image ).'">';
    <td> <button class="update" onclick="updateBook(<?php echo $id?>)">Update</button></td> 
        <td><a href="delete_rows.php?del=$row[ISBN]">delete</a></td>
  </tr>
    <?php } ?>
        </table>

        <form id="UPDT" name="updateform" action="update_books.php" method="POST">

            ISBN:<input type="text" name="ISBN" id ="ISBN" value="<?php echo $books->$ISBN;?>">
            Title:<input type="text" name="Title"  id="Title" value="<?php echo $books->$Title;?>">
            Author's name<input type="text" name="Authorsname" id="Authorsname" value="<?php echo $books->$Authorsname;?>">
            Edition<input type="text" name="edition" id="edition" value="<?php echo $books->$edition;?>">
            Year:<input type="text" name="year" id="year" value="<?php echo $books->$year;?>"> 
            Category: <input type="text" name="category" id ="category" value="<?php echo $books->$category;?>">
            Publisher:<input type="text" name="publisher" id="publisher"  value="<?php echo $books->$publisher;?>">
            Quantity-in-stock:<input type="text" name="quantityinstock" id ="quantityinstock" value="<?php echo $books->$quantityinstock;?>">
            Price:<input type="text" name="price" id="price" value="<?php echo $books->$price;?>">
            <input type="submit" value="Send" name="send">
        </form>

    </body>
</html>

Can someone please help!

  • What is the value of the variable $row[ISBN], and how the link looks?

  • I don’t know if I understand your question, I will try to answer the value of the variable $Row[ISBN] is $ISBN the link simply says "deleted successfully" but in reality did not erase anything

  • In your html, as the link inside the tag a?

  • Hello I edited the code and I put all the code where the <a> tag is, sorry it is so long but the <a> tag finds before the <form> almost at the end.

  • 1

    I think your mistake is here, <td><a href="delete_rows.php?del=$row[ISBN]">delete</a></td>, he’s playing the variable $row['ISBN'], as normal text, try: <td><a href="delete_rows.php?del=<?php echo $row['ISBN'];?>">delete</a></td>

  • tried and did not give :( the same happened - "deleted successfully" but the data remains in the database

  • Then in your file 'delete_rows.php' makes a echo in the variable $sql and test it on the bench. I’m pretty sure it’s passing a wrong amount or no value.

  • hi, did echo and returned the following: you have deletedDELETE FROM Books WHERE ISBN=' Notice: Undefined variable: Row in /home/unn_w17015779/public_html/update.php on line 84 ' in this case the line 84 is the line <a>

  • As I said, the problem is time to assemble your link, in the variable $row['ISBN'], I suggest once you load the html, press F12 and Inspecione the element a, in particular the part of del=algumvaloraqui.

  • I press F12 and do nothing, any more suggestions pf?

  • Well, then, the right thing is as Max Rogério indicated. After you make this change indicated by it, do the following: After --- $ISBN = $_GET['del']; ---- put these two lines echo $ISBN; Exit(); Run your script it will print the value and stop the execution and see if the value of this variable returned any value.

  • 1

    We will continue discussion on chat - http://chat.stackexchange.com/rooms/57375/problema-php-delete

  • 2

    I can see what you say but I can’t answer! po Anderson that worked and is working VALEUUU

  • "You must have 20 Reputation on The Stack Exchange Network" says I have to have this in order to be able to chat

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.