简体   繁体   中英

Update a html table form using php and mysql

i have created a php program to save books details. As well as i created function to update that saved data.but when i try to edit and save edited data nothing happen. Even any error is not shown by program. I used MySQL database. Please help me to solve this problem. I have mentioned my code below.

Here displaybook.php page shows the all books details which i have saved.Edit button also there.

editbook.php page contain the update query and save query.

displaybook.php code

<form method="POST" action="editbook.php">
    <table class="table table-striped" cellspacing="10">
        <thead>
            <tr>
                <th style="width:30%" scope="col"> BOOKID </th>
                <th style="width:30%" scope="col"> Title </th>
                <th style="width:50%" scope="col"> ISBN </th>
                <th style="width:30%" scope="col"> Author Name </th>
                <th style="width:30%" scope="col"> Publisher Name </th>
                <th style="width:30%" scope="col"> Category </th>
                <th style="width:30%" scope="col"> No of pages </th>
                <th style="width:30%" scope="col"> Published year </th>
                <th style="width:30%" scope="col"> Price</th>
                <th style="width:30%" scope="col"> Language </th>
             </tr> 
         </thead>
         <tbody>
         <?php
         while($row = mysqli_fetch_array($query)){
         ?>
             <tr>
                 <td><?php echo $row['bookid']; ?></td>
                 <td><?php echo $row['title']; ?></td>
                 <td><?php echo $row['isbn']; ?></td>
                 <td><?php echo $row['authorname'];?></td> 
                 <td><?php echo $row['publishername'];?></td>
                 <td><?php echo $row['category'];?></td>
                 <td><?php echo $row['noofpages'];?></td>
                 <td><?php echo $row['publishedyear'];?></td> 
                 <td><?php echo $row['price'];?></td>
                 <td><?php echo $row['language'];?></td>
                 <td><button type="submit" name="edit" class="btn btn-outline-secondary btn-sm" value="<?php echo $row['bookid']; ?>">Edit </button></td>
                 <td><button type="submit" name="delete" class="btn btn-outline-secondary btn-sm" value="<?php echo $row['bookid']; ?> ">Delete </button></td>
             <?php echo "<tr>";
             }
             ?>
         </tbody>
     </table>
 </form>

editbook.php code

<?php
if(isset($_POST['edit'])){
    $edit_id = $_POST['edit'];
    if (!$conn) {
        die("Failed to connect to database" . mysqli_connect_error());
    } else {
        $query = "SELECT * FROM book WHERE bookid='$edit_id'";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_array($result);
    }
?>
<form name="reg-frm" action="editbook.php" method="POST">
    <table border="0" cellspacing="1" cellpadding="1">
        <tbody>
            <tr>
                <td><span class="required"> </span>Title&nbsp;</td>
                <td><input id="name" type="text" name="title" value="<?php echo $row['title']; ?>" pattern="[A-Za-z].{2,}" placeholder="Title" title="eg:- Ayesha" required/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>ISBN&nbsp;</td>
                <td><input id="name2" type="text" name="isbn" value="<?php echo $row['isbn']; ?>"   placeholder="ISBN" title="eg:- 23478890034477" required/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Author Name&nbsp;</td>
                <td><input id="name3" type="text" name="authorname" value="<?php echo $row['authorname']; ?>" pattern="[A-Za-z].{2,}" placeholder="Author name" title="eg:- Ayesha" required/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Publisher Name&nbsp;</td>
                <td><input id="name4" type="text" name="publishername" value="<?php echo $row['publishername']; ?>" pattern="[A-Za-z].{2,}" placeholder="Publisher name" title="eg:- Ayesha" required/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Category&nbsp;</td>
                <td><input id="phone" type="text" name="category" value="<?php echo $row['category']; ?>" pattern="[A-Za-z].{2,}" placeholder="Category" title="eg:- English" required/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>No of Pages&nbsp;</td>
                <td><input id="name5" type="text" name="noofpages" value="<?php echo $row['noofpages']; ?>" /></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Pubished Year&nbsp;</td>
                <td><input id="phone2" type="text" name="publishedyear" value="<?php echo $row['publishedyear']; ?>" /></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Price</td>
                <td><input id="phone3" type="text" name="price" value="<?php echo $row['price']; ?>"/></td>
            </tr>
            <tr>
                <td><span class="required"> </span>Language</td>
                <td><input id="name6" type="text" name="language" value="<?php echo $row['language']; ?>" pattern="[A-Za-z].{2,}" placeholder="Language" title="eg:- Tamil" required/></td>
            </tr>
        </tbody>
    </table></br>
    <div style="text-align: center;">
        <input type="submit" value="SAVE" name="save" class="submit-btn"/>
    </div>
</form>
<?php
}
if(isset($_POST['save'])){
    $title=$_POST['title'];
    $bookid=$_POST['bookid'];
    $isbn=$_POST['isbn'];
    $authorname=$_POST['authorname'];
    $publishername=$_POST['publishername'];
    $category=$_POST['category'];
    $noofpages=$_POST['noofpages'];
    $year=$_POST['publishedyear'];
    $price=$_POST['price'];
    $language=$_POST['language'];

    $query1 = "UPDATE book SET title='$title',isbn='$isbn',authorname='$authorname',publishername='$publishername',category='$category',noofpages='$noofpages',publishedyear='$year',price='$price',language='$language' WHERE bookid='$bookid'";
    if(mysqli_query( $conn, $query1 )){
        header('location:displaybook.php');
    } else {
        echo mysqli_error ($conn);
    }
}
?>

The main reason for not updating is that the bookid value is missing from form reg-frm in the editbook.php file. It should be included, possibly in the first row of the table as <input type="hidden" name="bookid" value="<?php echo $row['bookid']; ?>" /> : a hidden field that you would not want to update/change.

Otherwise there also seems to be a typo in the editbook.php file where it reads:

</form>
<?php
}
if(isset($_POST['save'])){

It should be reading

</form>
<?php
if(isset($_POST['save'])){

without the first bracket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM