简体   繁体   English

PHP - 如何提交包含指向不同 MySQL 行的输入字段的表单

[英]PHP - How to submit a form containing input fields that point to different MySQL rows

I am setting up a form using this PHP that loops through all records a user may have:我正在使用此 PHP 设置一个表单,该表单遍历用户可能拥有的所有记录:

    <?php foreach ($items as $row): ?>
            <tr>
                <td>
                    <?php echo form_hidden('id', $row->id); ?>
                </td>
                <td>
                    <?php echo '<strong>' . $row->name . '</strong>'; ?>
                </td>
                <td>
                    <?php echo form_input('number', $number); ?>
                </td>
                <td>
                    <?php echo form_input('registry', $registry); ?>
                </td>
                <td>
                    <?php echo form_checkbox('OK', $ok, $ok); ?>
                </td>
            </tr>
    <?php endforeach; ?>

This gives me a form with the following look:这给了我一个具有以下外观的表格:

在此处输入图像描述

The idea here is that each row belongs to a unique ID/row in the database, and I would like to allow the user to edit all on the same page/form, using a single submit button.这里的想法是每一行都属于数据库中的一个唯一 ID/行,我希望允许用户使用单个提交按钮在同一页面/表单上编辑所有内容。

What would be the best way of implementing this?实现这一点的最佳方法是什么?

When this data is submitted, there should be a way of looping through each packet of information (from each user) in my controller.提交此数据时,应该有一种方法可以循环遍历我的 controller 中的每个信息包(来自每个用户)。 Would this be done via ajax/json?这会通过 ajax/json 完成吗?

There's no need to use ajax mate.无需使用 ajax 伴侣。

For each put a hidden input with the ID of the row in this format:对于每一个以这种格式放置一个带有行 ID 的隐藏输入:

<input type="hidden" name="id[<?= $row->id ?>]" value="<?= $row->id ?>" ?>

Do the same for each element in the tr, ie name them as对 tr 中的每个元素执行相同的操作,即将它们命名为

name="number[<?= $row->$id ?>]"
name="registry[<?=$row->$id ?>]"
name="ok[<?=$row->$id ?>]"

and once you post the FORM you can iterate each row with:一旦您发布了表格,您就可以使用以下命令迭代每一行:

foreach ($_POST['id'] as $key => $value) {
    echo $_POST['name'][$key];
}

This does not use codeigntier, but you should be familiar with the general technique before attempting to use CI to shortcut this process.这不使用 codeigntier,但在尝试使用 CI 来缩短此过程之前,您应该熟悉一般技术。 Codeigniter will help you with rendering the form elements, performing validation, escaping your input and performing your query - but it will only help you (do anything) if you understand the basic principles involved. Codeigniter 将帮助您呈现表单元素,执行验证,escaping 您的输入和执行您的查询 - 但它只会帮助您(做任何事情)如果您了解所涉及的基本原则。 Hope this helps希望这可以帮助

MARKUP标记

<form action="/process.php">
<div>
    <h2>GORDON</h2>
    <input type="text" name="user[1][number]" /> <!-- The number corresponds to the row id -->
    <input type="text" name="user[1][registry]" />
    <input type="checkbox" name="user[1][ok]" value="1" />
</div>
<div>
    <h2>ANDY</h2>
    <input type="text" name="user[242][number]" />
    <input type="text" name="user[242][registry]" />
    <input type="checkbox" name="user[242][ok]" value="1" />
</div>
<div>
    <h2>STEWART</h2>
    <input type="text" name="user[11][number]" />
    <input type="text" name="user[11][registry]" />
    <input type="checkbox" name="user[11][ok]" value="1" />
</div>

<input type="submit" />

PHP PHP

$users = $_REQUEST['user'];

foreach ($users as $rowId => $info){

    // YOU SHOULD MAKE SURE TO CLEAN YOUR INPUT - THIS IS A GUESS AT WHAT YOUR DATA TYPES MIGHT BE
    $id = (int) $rowId;
    $number = (int) $info['number'];
    $registry = mysql_real_escape_string($info['registry']);
    $ok = (int) ($info['ok']);

    $q = "UPDATE user SET number = $number, registry = '$registry', ok = $ok WHERE id = $id";
    mysql_query($q);

    // You may want to check that the above query was sucessful and log any errors etc.

}

You need to set up input-names as array-names, so you will send the whole form and may iterate over the entries.您需要将输入名称设置为数组名称,因此您将发送整个表单并可能遍历条目。

eg例如

<?php
echo form_input('userdata[' . $row->id . '][number]', $number);
?>

which would possibly create an这可能会创建一个

<input name="userdata[1][number]" />

(I don't know where those form-functions came from…) (我不知道那些表单函数是从哪里来的……)

This will result in an array $_POST['userdata'] which may be iterated via:这将产生一个数组$_POST['userdata']可以通过以下方式迭代:

foreach($_POST['userdata'] as $userId => $userInputFields)
{
    $user = new User($userId);
    $user->number = $userInputFields['number'];
    // …
}

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

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