繁体   English   中英

使用php和mysql更新html表形式

[英]Update a html table form using php and 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);
    }
}
?>

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

否则,在editbook.php文件中似乎还有一个错字:

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

应该在看

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

没有第一个括号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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