使用相同的 php 表单添加/查看/编辑 mysql 条目
我编写了一个 PHP 表单来将条目添加到 MySQL 数据库中,该数据库看起来像这里的第一张图片 。但是,我还想使用相同的表单来查看和编辑数据库中已有的信息,如第二张和第三张照片所示。最简单的方法是什么?
I've written a PHP form to add entries into a MySQL database that looks like the first picture here. However, I also want to use the same form to view and edit information already in the database as shown in the second and third photos. What's the simplest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用相同的形式通常很容易。另一个问题是如何解决验证错误而不是让用户重新填写表单 - 这可以在同一过程中解决。您将需要一些用于选择字段和任何复选框/单选字段的帮助程序。帮助程序的确切工作量取决于您,但这些将帮助您入门:
复选框帮助程序几乎完全相同。选择选项非常相似:
在表单中使用它们如下所示:
表单部分可以是相同的 - 它只需要一个名为 $data 的数组进入其中。该数组可以为空,可以从数据库填充,也可以从无效的表单数据填充。
It's generally quite easy to use the same form. Another question is how you solve validation errors and not making the user re-fill the form - this gets solved as in the same process. You'll need some helpers for the select fields and any checkbox/radio fields. Exactly how much the helpers do is up to you, but these will get you started:
The checkbox helper's pretty much the exact same. Select options are pretty similar:
Using them in the form follows something like this:
The form part can be the same - it just needs an array named $data going into it. The array can be empty, populated from the database, or from form data that was invalid.
检查数据库中该字段的值是否为空,如果是,则不要在该字段中填写值:
对于下拉列表,创建一个与下拉列表中的值匹配的值表,然后如果有的话,您可以匹配您的 ID 并打印如下内容:
This is a相当广泛的答案,针对一个相当广泛的问题。
Check to see if the value of the field is null in the database, if so, then don't fill in a value in the field:
For drop downs, make a table of values that would match the values in the drop down, then you can match your id if there is one and print something like this:
This is a rather broad answer for a rather broad question.