如何将 mysql_insert_id() 传递给 $_POST 或表单操作?
我有一个表单页面,其中提交 INSERT 或 UPDATE 查询,具体取决于 ID 的存在/不存在(当存在 ID 时,它用于检索记录并预填充表单)。无论哪种情况,处理都是在 form.php 中进行的,因此表单的操作就是其本身 (action="/form.php">
)。我的问题是,当 form.php 重新加载提交后时,URL 的 ID 为空,因此页面进入“INSERT”模式,而不是“UPDATE”模式。解决这个问题的最佳实践方法是什么?
我应该向这个“if”添加什么运算符/条件...
if (isset($_GET['ID']) && is_numeric($_GET['ID'])) {
... 包括提交后空 ID URL(即form.php?ID=
)
或者,
如何将 `$newID = mysql_insert_id();1 传递给表单的操作? (我在这里尝试了多种变体,但没有成功)
$newID = mysql_insert_id();
...[剪断]...
我正在阅读有关隐藏输入和会话的信息但我还不清楚如何使用其中任何一个来解决这个问题。最后,由于重新加载表单页面并不是绝对必要的,因此我越来越倾向于将表单处理/数据库查询移动到另一个页面(例如,process.php)以希望简化;对此有何意见?最佳/常见做法是什么?
提前非常感谢,
svs
I have a form page in which either an INSERT or an UPDATE query is submitted, depending on the presence/absence of an ID (and when there's an ID it's used to retrieve the record and pre-populate the form). In either case, the processing is in form.php so the form's action is itself (action="/form.php">
). My problem is that when form.php reloads post-submit, the URL has an empty ID so the page enters 'INSERT' mode, rather than 'UPDATE' mode. What's the best practice way to resolve this?
What operator/condition should I add to this 'if' ...
if (isset($_GET['ID']) && is_numeric($_GET['ID'])) {
... to include post-submit empty ID URL (i.e., form.php?ID=
)
OR,
How do I pass `$newID = mysql_insert_id();1 to the form's action? (I've tried a number of variations here w/out success)
$newID = mysql_insert_id();
... [ snip ] ...<form method="post" action="/html/form.php?ID=<?php echo $newID; ?>">
I'm reading about hidden inputs and sessions but it's not yet clear to me how to use either to solve this problem. Lastly, since it isn't absolutely necessary that I reload the form page, I'm increasingly tempted to move the form processing/db queries to another page (e.g., process.php) to hopefully simplify; any opinions on this? What's best/common practice?
Many thanks in advance,
svs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常见的做法应该是将数据发布与数据显示分开。这可以防止用户首次到达页面时意外添加内容,以及用户点击刷新时意外重复发布。
此外,保持逻辑分离可以使代码在将来更具可读性和可维护性。
您可能应该寻找的方法是:
编辑:虽然我在 process_add.php 上有 GET 参数,但它们只是用来证明它们正在被传递。它们应该在实际实现中作为 POST 参数发送。
Common practice should be to keep data posting separate from data displaying. This prevents accidental adds on a user's first arrival to the page as well as accidental double-posts if the user hits refresh.
In addition, keeping the logic separate makes the code more readable and maintainable in the future.
The approach you should probably look for is:
EDIT: While I have GET arguments on process_add.php, they are only there to demonstrate that they are being passed. They should be sent as POST arguments in and actual implementation.
这是使用模板的此类代码的示例。
工作 CRUD 应用程序基于传递 id 的想法
,但不知道为什么你需要传递新生成的 id。
模板:
form.php
和 list.php:
here is an example of such a code, using templates.
working CRUD application based on the idea of passing id
dunno, though, why do you need to pass freshly generated id.
templates:
form.php
and list.php: