PHP-无法从HTML表单提交访问POST值

时间:2018-10-15 06:21:50

标签: php html mysql mariadb

尝试提交表单表时,我收到此错误(4次):

  

通知:未定义的索引:>第106行的php / ChartOfAccountsInclude.php中的accountid

这是我代码中的第106行

$account_id = $_POST["accountid"][$i];

我正在尝试从本地数据库加载表数据。然后,我允许用户访问某些字段以更改值。完成后,他们按按钮提交,这就是我收到上述错误的时间。数据库中当前有四个帐户,这意味着我的代码每次循环遍历POST值时,都无法检索到帐户ID。我已经粘贴了下面的相关代码:

用户页面 ChartOfAccounts.php

<?php
    include("view/SiteHeader.php");
    include('php/ChartOfAccountsInclude.php');
?>

<html>
    <head>
        <title>Chart of Accounts</title>
    </head>

    <body>
        <br/>
        <h2>Chart of Accounts</h2>
        <form action="" method="post">
            <div>
                <?php retrieveChart() ;?>
            </div>

            <div class="container">
                <button type="submit" name="submitMods" class="btn btn-outline-secondary">Submit Account Modifications</button>
            </div>
        </form>
        <?php if($submit_err) : ?>
            <?php generateError('warning', 'Account could not be updated.') ?>
        <?php endif; ?>
    </body>
</html>

PHP Include ChartOfAccountsInclude.php

<?php
    include_once('php/session.php');
    $sql = "SELECT * FROM Accounts";
    $result = mysqli_query($db,$sql);
    $count = mysqli_num_rows($result);
    $submit_err = FALSE;
    $submitaccount_err = FALSE;

    function retrieveChart() {
        global $db;
        $funcSQL = "SELECT * FROM Accounts";
        $funcResult = mysqli_query($db,$funcSQL);

        echo '<table border="1" class="table table-bordered table-hover">
        <thead>
        <tr>
            <th>Account ID</th>
            <th>Account Name</th>
            <th>Date Created</th>
            <th>Type</th>
            <th>Term</th>
            <th>Status</th>
            <th>Created By</th>
        </tr>
        </thead>';

        while($row = mysqli_fetch_array($funcResult))
        {
            $current = $longterm = $active = $inactive = $debi = $credit = $asset = $expense = $liability = $equity = $revenue = "";
            $curusr = $row['user_id'];
            $getUsernameSQL = "SELECT username FROM User_accounts WHERE user_id = $curusr";
            $usernameResult = mysqli_query($db,$getUsernameSQL);
            $usernameRow = mysqli_fetch_array($usernameResult);

            switch ($row['term']) {
                case 'Current':
                    $current = "selected = \"selected\"";
                    break;
                case 'Long Term':
                    $longterm = "selected = \"selected\"";
                    break;
            }
            switch ($row['account_status']) {
                case 'Active':
                    $active = "selected = \"selected\"";
                    break;
                case 'Inactive':
                    $inactive = "selected = \"selected\"";
                    break;
            }
            switch ($row['type']) {
                case 'Asset':
                    $asset = "selected = \"selected\"";
                    break;
                case 'Expense':
                    $expense = "selected = \"selected\"";
                    break;
                case 'Liability':
                    $liability = "selected = \"selected\"";
                    break;
                case 'Equity':
                    $equity = "selected = \"selected\"";
                    break;
                case 'Revenue':
                    $revenue = "selected = \"selected\"";
                    break;
            }

            echo '<tr>';
                echo '<td> <input name="accountid[]" disabled value="' . $row['account_id'] . '"> </td>';
                echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
                echo '<td> <input name="datecreated[]" disabled value="' . $row['date_created'] . '"> </td>';
                echo '<td> 
                        <select name="type[]">
                            <option '.$asset.'>Asset</option>
                            <option '.$expense.'>Expense</option>
                            <option '.$liability.'>Liability</option>
                            <option '.$equity.'>Equity</option>
                            <option '.$revenue.'>Revenue</option>
                        </select>
                    </td>';
                echo '<td> 
                        <select name="term[]">
                            <option '.$current.'>Current</option>
                            <option '.$longterm.'>Long Term</option>
                        </select>
                    </td>';
                echo '<td> 
                        <select name="accountstatus[]" value="' . $row['account_status'] . '">
                            <option '.$active.'>Active</option>
                            <option '.$inactive.'>Inactive</option>
                        </select>
                    </td>';
                echo '<td>
                        <input name="user_id[]" disabled value="' . $usernameRow['username'] . '">
                    </td>';
            echo '</tr>';
        } 
        echo '</table>';
    }

    if( isset($_POST['submitMods']) ) {
        $i = 0;

        while($i < $count) {
            $account_id = $_POST["accountid"][$i];
            $account_name = mysqli_real_escape_string($db,$_POST['accountname'][$i]);
            $type = mysqli_real_escape_string($db,$_POST['type'][$i]);
            $term = mysqli_real_escape_string($db,$_POST['term'][$i]);
            $account_status = mysqli_real_escape_string($db,$_POST['accountstatus'][$i]);

            $updateAccountsql = "
                UPDATE Accounts
                SET
                    account_id = '$account_id',
                    account_name = '$account_name',
                    type = '$type',
                    term = '$term',
                    account_status = '$account_status'
                WHERE account_id = '$account_id'";

            if($account_name == "") {
                $submit_err = TRUE;
            } else {
                $updateResult = mysqli_query($db,$updateAccountsql);
            }
            $i++;
        }

        if(!$submit_err) {
            //header("Location: ChartOfAccounts.php");
        }
    }
?>

4 个答案:

答案 0 :(得分:4)

具有禁用属性的HTML元素中未发布,或者您可以说未提交其值。

您的输入元素如下,因此不会发布任何值。

<input name="accountid[]" value="' . $row['account_id'] . '" disabled /> 

禁用的控件限制:

  • 它不会获得焦点。

  • 在标签导航中将被跳过。

  • 无法成功发布。

因此如果您不想让用户编辑该输入,并且不需要向用户显示该输入,则可以尝试使用input type="hidden" ,如下所示:

<input type="hidden" name="accountid[]" value="' . $row['account_id'] . '" /> 

如果您想向用户显示ID但又不想让他们编辑,则可以使用您的案例中的readonly属性,通过readonly属性,您可以发布字段的数据,参见以下示例:

<input type="text" name="accountid[]" value="' . $row['account_id'] . '" readonly /> 

只读元素

  • 可以接收焦点,但不能被用户修改。
  • 它将包含在标签导航中。
  • 它将成功发布。
  • 不适用于复选框并选择标签

Reference answer

答案 1 :(得分:1)

在行下方更改

<input name="user_id[]" disabled value="' . $usernameRow['username'] . '">

<input name="user_id[]" type="hidden" value="' . $usernameRow['username'] . '">

答案 2 :(得分:1)

因为accountid为禁用字段,所以它不提交并且在$ _POST变量中不存在,因此您需要用隐藏字段替换文本框

答案 3 :(得分:0)

从输入字段中删除了disabled

替换

echo '<td> <input name="accountid[]" disabled value="' . $row['account_id'] . '"> </td>';
echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
echo '<td> <input name="datecreated[]" disabled value="' . $row['date_created'] . '"> </td>';

echo '<td> <input name="accountid[]" value="' . $row['account_id'] . '"> </td>';
echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
echo '<td> <input name="datecreated[]" value="' . $row['date_created'] . '"> </td>';

如果您不允许别人编辑字段,则可以使用readonly代替disabled