php表单提交按钮

发布于 2024-08-24 13:46:44 字数 270 浏览 12 评论 0原文

我对 php 比较陌生。我有以下问题。 假设我有一个页面,

  • 其中的表单有两个字段 x、y 和两个按钮:提交和清除;
  • 一个表格,用两个按钮显示 x、y 的数据库记录,

当我在表单字段中输入值并按下按钮时编辑和删除,提交将数据插入数据库中;数据如下表所示;

当我在表格上按编辑时,表单将填充所选记录中的数据。现在我想提交更新记录,而不仅仅是插入新记录。

我应该如何进行?

谢谢!!!

朱塞佩

I am relatively new to php. I have the following problem.
Suppose I have a page with

  • a form with two fields x, y and two buttons: submit and clear;
  • a table that shows the db records for x, y with two buttons, edit and delete

when I enter values in the form fields and press button, submit inserts the data in the db; data is then shown in the table below;

when I press edit on the table, the form is populated with the data from the selected record. Now I want submit to update the record and not just insert a new one.

How should I proceed?

Thanks!!!

Giuseppe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫九 2024-08-31 13:46:44

您应该在表单中添加一些隐藏字段来区分更新和插入。

一个好的方法是,例如,将主键 has field

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

放在 PHP 代码上,您可以

if(isset($_POST['id']) && $_POST['id'] != 0){
 // this is an update 
 $sql = "UPDATE ...."
 ...
} else {
 $sql = "INSERt ...";
 ...
}

对插入表单执行类似的操作,只是不要放置隐藏输入或将值设置为 0

you should add some hidden field in your form to differentiate the updates and insert.

A good way to do that is for example to put your primary key has field

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

after on the PHP code you can do something like this

if(isset($_POST['id']) && $_POST['id'] != 0){
 // this is an update 
 $sql = "UPDATE ...."
 ...
} else {
 $sql = "INSERt ...";
 ...
}

for the insert form just don't put the hidden input or make the value being 0

余厌 2024-08-31 13:46:44

首先,您应该将当前编辑的 id 存储在隐藏字段或查询字符串之类的地方,一旦完成,您应该使用 update 语句来更新记录,而不是插入新记录,例如:

// sql query
update tablename set fieldname = 'fieldvalue'......... and son

您需要显示您的代码才能获得准确的答案。

First of you should be storing id of the current edit somewhere like hidden field or query string, once you have done that, you should use the update statement to update the record rather than inserting new one something like:

// sql query
update tablename set fieldname = 'fieldvalue'......... and son

You need to show your code for getting accurate answer.

烟雨扶苏 2024-08-31 13:46:44
  1. 向行添加标识符:数据库中的 ID 列(最好是无符号整数),该列也在表中(您可以将其用作用于编辑条目的 url 中的查询字符串),并作为隐藏输入在表单中
  2. 如果要添加条目,请确保隐藏输入设置为 null 或零(或不能是有效标识符的其他值)
  3. 每当提交表单时,测试标识符是否为 null 或其他 值else
  4. 如果标识符为空,则像之前一样添加
  5. 如果标识符不为空,则使用输入中的标识符更新元素
  1. Add an identifier to your rows: an ID column in the database (preferably an UNSIGNED INTEGER), which is also in the table (you can use it as a querystring in the url you use for editing an entry), and as a hidden input in the form
  2. If you're adding an entry, make sure the hidden input is set to null, or zero (or some other value that cannot be a valid identifier)
  3. Whenever the form is submitted, test for the identifier to be null or something else
  4. If the identifier is null, add like you did before
  5. If the identifier is not null, update the element with the identifier from the input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文