使用php和mysql更新html表格

时间:2017-11-10 09:40:56

标签: php html mysql

我创建了一个用于保存书籍详细信息的php程序。以及我创建的功能来更新保存的数据。但是当我尝试编辑和保存编辑的数据时,没有任何事情发生。程序不会显示任何错误。我使用MySQL数据库。请帮我解决这个问题。我在下面提到了我的代码。

此处displaybook.php页面显示了我已保存的所有图书详细信息。此处还有编辑按钮。

editbook.php页面包含更新查询和保存查询。

displaybook.php代码

<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代码

<?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);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

不更新的主要原因是bookid文件中的reg-frm格式中缺少editbook.php值。它应该包括在内,可能在表格的第一行<input type="hidden" name="bookid" value="<?php echo $row['bookid']; ?>" />:一个您不想更新/更改的隐藏字段。

否则editbook.php文件中似乎也会出现拼写错误:

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

应该是阅读

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

没有第一个括号。