更新形式中未定义的索引错误

时间:2017-11-23 07:23:57

标签: php

this img contain my updation.php code

this img contain my updation.php code

当我将数据插入文本框然后单击“提交”时,

javascript alertbox sys"您的数据已成功更新"而不是浏览器显示错误消息

error messeges

<?php
    $con = mysqli_connect('127.0.0.1','root','','');
    mysqli_select_db($con,'brm_db');

    $q = "SELECT * FROM book";

    $result = mysqli_query($con,$q);

    $num = mysqli_num_rows($result);

    mysqli_close($con);
?>


<!DOCTYPE html>
<html>
    <head>
        <title> Update Record </title>
        <link rel="stylesheet" href="./css/viewstyle.css" />
    </head>
    <body>
        <h1 align="center"> Book Record Management</h1>
    <center>
        <form action="updation.php" method="post">
        <table id="view_table">
            <tr>
                <th>Book Id</th>
                <th>Title</th>
                <th>Price</th>
                <th>Author</th>
            </tr>

            <?php
                for($i=1;$i<=$num;$i++)
                {
                    $row = mysqli_fetch_array($result);
            ?>
            <tr>
                <td> <?php echo $row['bookid'];?>
                    <input type="hidden" name="bookid <?php echo $i ;?>" value="<?php echo $row['bookid'];?>" /> </td>

                <td><input type="text" name="title <?php echo $i ;?> "value="<?php echo $row['title'];?>" /></td>
                <td><input type="text" name="price <?php echo $i ;?> "value="<?php echo $row['price'];?>" /></td>
                <td><input type="text" name="author <?php echo $i ;?> "value="<?php echo $row['author'];?>" /></td>
            </tr>
            <?php
                }
            ?>
            </table>
            <input type="submit" value="Update" style="background-color:lightgreen;width:100px;" />
            </form>
        </center>
    </body>

</html> 

1 个答案:

答案 0 :(得分:0)

声明在进入for循环之前分配给那些的数组,如下所示

$bookid = array();

$title = array();

$price = array();

$author = array();

//for loop goes here

而不是这种类型,我建议你有一个如下的单个数组,然后存储在其中

$books = array();

for($i = 0; $i <= $records; $i++){
    $books[$i]['bookid'] = $_POST[$index1];
    $books[$i]['title'] = $_POST[$index1];
    $books[$i]['price'] = $_POST[$index1];
    $books[$i]['author'] = $_POST[$index1];
}

现在按如下方式循环上面的$ books数组,这将非常方便实现

foreach($books as $book){

}