-1

I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.

Here i am passing item id through the URL in order to select the item

Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.

Here is the Button that opens the Popup Modal

<?php 
$res= mysqli_query($db,"SELECT* FROM `books`;");

if(mysqli_num_rows($res)>0){
  while($row= mysqli_fetch_assoc($res)){
    if($row!=0)
    {
    $bid= $row['book_id'];
 ?>
<ul>
<li>
 <a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>


<!--Here is the Popup Modal for Edit -->

<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="updatebook.php" method="POST">
          <div class="form-group">
            <label for="isbn" class="col-form-label">ISBN</label>
            <input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
          </div>
          <div class="form-group">
            <label for="title" class="col-form-label">Enter Book Title</label>
            <input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
          </div>
            <div class="form-group">
            <label for="author" class="col-form-label">Enter Author Name</label>
            <input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
          </div>
            <div class="form-group">
            <label for="image" class="col-form-label">Image URL</label>
            <input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
          </div>
            <div class="form-group">
            <label for="description" class="col-form-label">Enter Book Description</label>
            <textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
          </div>
            <div class="form-group">
            <label for="url" class="col-form-label">Book URL</label>
            <input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <a href="updatebook.php?id=<?php echo $row['book_id']?>"><button type="button" class="btn btn-primary" name="save">Save Changes</button></a>
      </div>
    </div>
  </div>
</div>


<?php 
}
}
}

else
{
   echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
 }
?>

</ul>

Here is the updatebook.php Page

<?php

include "connection.inc.php";

if(isset($_POST['save']))
{
    $id= $_POST['id'];
    $isbn= $_POST['isbn'];
    $title= $_POST['title'];
    $author= $_POST['author'];
    $image= $_POST['image'];
    $desc= $_POST['description'];
    $url= $_POST['url'];

$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res) 
{
    <script type="text/javascript"> 
        alert("Data Updated Successfully"); 
        window.location.href= "home.php";
    </script>
}
else
{
    <script>
    alert("Not Updated!");
        window.location.href="home.php";
    </script>
}
<?php
}
?>

Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.

  • Don't know why I am not able to update the item. Please Help! – codewithdev Apr 14 '21 at 17:54
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Apr 14 '21 at 17:55
  • Why have you wrapped your "Save" button in a hyperlink? That makes no sense. It will cause a GET request, but your PHP expects a POST. it isn't submitting your form properly. Get rid of the a href, turn the button into type="submit" and try again. Any PHP/HTML forms tutorial would show you the standard way to structure a form, including the submit button. I'm not sure why you decided to take a different approach. – ADyson Apr 14 '21 at 18:00
  • @ADyson Okay, let me check – codewithdev Apr 14 '21 at 18:01
  • The "Save" button. That's the only one wrapped in a hyperlink as far as I can see! – ADyson Apr 14 '21 at 18:02
  • Now changing button type= "submit" is not working. Clicking over "save changes" is not working anymore. – codewithdev Apr 14 '21 at 18:06
  • Did you remove the hyperlink too? Show your new code in the question. Also "not working" isn't a useful description of anything. Explain specifically what happened – ADyson Apr 14 '21 at 18:07
  • I have changed this `` – codewithdev Apr 14 '21 at 18:08
  • 1
    @codewithdev the submit button is not in form as well – Yasir Mushtaq Apr 14 '21 at 18:09
  • That's an excellent point. move the button inside the form. Again, every forms tutorial in the world would show it like that already. – ADyson Apr 14 '21 at 18:15
  • Also you need a (hidden?) field for `id` so it will be submitted with the rest of the form data. – ADyson Apr 14 '21 at 18:17
  • I have placed my form closing tag correctly but still its not working. And whenever i try to click on "save changes" button it just prompt dialog for "ok" then for a millisecond an error coming on `updatebook.php` stating that `if($res){.......` something and then home page opens. – codewithdev Apr 14 '21 at 18:21
  • So, remove the window.location stuff for the moment, and then you might see the error. And/or configure PHP to log errors and warnings to a file. – ADyson Apr 14 '21 at 18:24
  • @ADyson So Its the error `Undefined index: id in C:\xampp\htdocs\E-library\home\updatebook.php on line 7 if($res) { } else { }` – codewithdev Apr 14 '21 at 18:24
  • Anyway actually... it's not just an error, it's just showing you the exact text your page outputs to the screen. The `if $res` is outside your PHP tags...so it's not seen as real code, just static text – ADyson Apr 14 '21 at 18:25
  • If you want more help, you'll need to update the question to show the latest version of your code, since we don't know what exact changes you have made overall. Details matter, as we've just observed. – ADyson Apr 14 '21 at 18:26
  • The undefined index is a warning rather than an error btw. But certainly the fact the value is missing will mean the query doesn't update anything. the answer below shows you how to use the hidden field properly. – ADyson Apr 14 '21 at 18:28
  • It's working perfectly Now. Thanks @ADyson – codewithdev Apr 14 '21 at 18:33
  • Thanks @YasirMushtaq for the addition to code. – codewithdev Apr 14 '21 at 18:34

1 Answers1

1
  1. Change your Save button type to "submit" and remove the <a> hyperlink.
  2. Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
  3. Take book_id in a hidden field so it will be the part of POST.

Look at the modal code below:

<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="updatebook.php" method="POST">
        <input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
        <div class="modal-body">
          <div class="form-group">
            <label for="isbn" class="col-form-label">ISBN</label>
            <input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
          </div>
          <div class="form-group">
            <label for="title" class="col-form-label">Enter Book Title</label>
            <input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
          </div>
            <div class="form-group">
            <label for="author" class="col-form-label">Enter Author Name</label>
            <input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
          </div>
            <div class="form-group">
            <label for="image" class="col-form-label">Image URL</label>
            <input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
          </div>
            <div class="form-group">
            <label for="description" class="col-form-label">Enter Book Description</label>
            <textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
          </div>
            <div class="form-group">
            <label for="url" class="col-form-label">Book URL</label>
            <input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary" name="save">Save Changes</button>
        </div>
      </form>
    </div>
  </div>
</div>
Yasir Mushtaq
  • 158
  • 1
  • 13