programmierfrage.com
Ask a question    Sign Up Sign In
  • Train typing speed
  • About/Contact
  • Privacy Policy
  1. Home
  2. php - How to Update Mysql Data with a button on Home Page that is passing id on URL?

227 votes
1 answers

php - How to Update Mysql Data with a button on Home Page that is passing id on URL?

Get the solution ↓↓↓

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.


Undefined asked
2021-12-5
Write your answer



902
votes

Answer

Solution:

  1. Change your Savebutton type to "submit" and remove the<a> hyperlink.
  2. Use<form> as a part of bothmodal-body andmodal-footer so the submit button will be part of it.
  3. Takebook_id in a hidden field so it will be the part ofPOST.

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>
Write your answer




Share solution ↓

Additional Information:

Date the issue was resolved:
2021-12-5
Link To Source
Link To Answer People are also looking for solutions of the problem: the browser (or proxy) sent a request that this server could not understand.

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Similar questions

Find the answer in similar questions on our website.

568 php - how to display custom attribute on category page in GRID and LIST mode
658 php - Insert file into mysql database as varbinary datatype
178 php - How do you avoid the same data in foreach, on sequential data?
500 php - How can I call data (array) which is in controller transfer to view page
605 database - Getting the db name or url used from models in php activerecord?
791 php - Passing second user model data to a controller in Laravel
396 codeigniter - When i'm transfering to another page getting data(s) (PHP Error Message:Trying to get property of non-object)
427 php - Getting specific part of value in mysql database
240 php - Solve PHP4 to PHP5 ugrade error
57 How to insert 100 records in mysql procedure and loop in MySQL procedure not in PHP?

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.






About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/


JavaScript

JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/


MySQL

DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL. It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/


Bootstrap

Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/


HTML

HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/



Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Yacht Magazine



Latest questions:

695 php - How to join and get data from two tables in laravel?

398 php - Complex regex request - need three variables from string

296 PHP header() not working because of character encoding

580 php - Adjusting BBcode parser to note parse smileys in URLs?

210 php - Two Steps to set Session as Logged In

531 Average time in hours,minutes and seconds in php

342 xml - php FOR not work

255 MySQL Search Limit using PHP

428 php - Solve PHP4 to PHP5 ugrade error

258 php - logic behind displaying time values with seconds and minutes range

Users questions:

NEW

mysql

NEW

Warning: Undefined array key "program" in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key "enter_course" in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key "enter_address" in E:\xampp\htdocs\pi\alumni.php on line 32

NEW

Rename existing images with keyword in WordPress PHP

NEW

codeigniter

NEW

Class "web_profiler.controller.profiler" does not exist after upgrading symfony 3.4 to 4.4 version




PHP x 410847
Laravel x 36785
Yii x 3846
CodeIgniter x 9997
Symfony x 7793
CakePHP x 3085
Zend Framework x 1235
Phalcon x 300
FuelPHP x 47
Slim x 570
JavaScript x 36883
React x 550
Angular x 1121
Vue.js x 181
JQuery x 11108
Backbone.js x 21
Node.js x 463
Ember.js x 10
Meteor x 7
Polymer x 20
Aurelia x 1
MySQL x 39074
CSS x 2497
Bootstrap x 1603
Foundation x 84
UIkit x 4
Semantic UI x 2
Bulma x 2
Animate.css x 1
HTML x 20978




© 2021-2023 Programming problem solving site for beginners and advanced. Answers to questions related to coding.

E-mail: [email protected]

This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK


OK