更新 mySQL 数据库时出现问题
好的,我已经成功地从我的数据库中检索了有字段的行:
ref,fname,lname,event1,event2,event3.
ref is the ID (autonumber).
因此,在我的索引 php 文件中,我从数据库中检索数据并将其存储在具有适当标题的表中,然后存储在我拥有的大约 100 个名称中。对于事件 1、事件 2 和事件 3 行,我有复选框。现在,我已经尝试了几个小时来捕获选中的复选框并更新 event1 和/或 event2 和/或 event3 的数据库记录列。我认为我的错误是当我命名复选框时,我为每一行命名:
我命名为 event1[]
的 event1 复选框 我将 event2 复选框命名为 event2[]
我将 event3 复选框命名为 event3[]
...并且重复了 100 次。当我提交表单时,我在 post_submit.php 文件中写入了以下内容:
$event1[] = $_POST['event1'];
$event2[] = $_POST['event2'];
$event3[] = $_POST['event3'];
这显然是错误的,因为它不起作用。
发生的情况是,当我选中一些复选框然后提交表单时,如果我选中 event1 的最后一个复选框,并且我转到我的数据库,并且只有 event1,那么数据库中的前几条记录始终会被更新。已更新的第一条记录的字段。我在这里做错了,我猜这与我上面提供的信息有关。有人可以帮忙吗?另外,对于复选框值,我有“1”,因此想法是如果选中了 event1,2,3 字段,则将其更新为 1。
谢谢
Okay, so I have successfully retrieved rows from my db where i have fields :
ref,fname,lname,event1,event2,event3.
ref is the ID (autonumber).
So in my index php file I retrieve the data from my db and store in a table with appropiate headers and then the 100 names approx I have. For event 1 event 2 and event 3 rows i have checkboxes. Now , I've tried for hours to capture the checked checkboxes and update the db record columns for event1 and/or event2 and/or event3. I think my error is when i name the checkboxes, I named for each row:
The event1 checkbox I named event1[]
The event2 checkbox I named event2[]
The event3 checkbox I named event3[]
...and this was repeated a 100 times. When I submit the form, I wrote in the post_submit.php file the following:
$event1[] = $_POST['event1'];
$event2[] = $_POST['event2'];
$event3[] = $_POST['event3'];
Which is apparently wrong as it doesn't work.
What happens is, when i check some checkboxes and then submit the form, it's always the first couple of records in my db that gets updated, if i check the LAST checkbox for event1, and i go to my database and it was only the event1 field for the first record which got updated. I'm doing something wrong here, and i'm guessing it's something to do with the information I have provided above. Can somebody please help. Also, for the checkbox value i have "1" so the idea is to update the event1,2,3 fields with a 1 if it was checked.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,您有一个大约 100 行的表,其中:
事件 1-3 只是表字段中的复选框。
如果您只有值为 1 或 0(选中或未选中)的 event1[],并且您发布此表单,则提交屏幕将只有一个由 0 和 1 组成的数组。它不跟踪它来自哪一行。
建议:为您的 event1-3[] 提供 id 的值。这样,您的事件数组将包含需要更新事件的 ID。
因此,如果您的 id = 3,则选中复选框:
If I understand correctly, you have a table of about 100 rows with:
and event 1-3 are just checkboxes in the table field.
If you just have event1[] with value either 1 or 0 (checked or unchecked) and you post this form, the submit screen will just have an array of 0's and 1's. It does not keep track of which row it comes from.
Advise: give your event1-3[] the value of the id. This way your event array will contain the id's that need the event updated.
So if you have id = 3, then have the checkbox:
如果我遵循您在这里尝试执行的操作,我认为您正在尝试收集选中的复选框的数组。考虑以下内容:
如果我检查 1,2 和 5,
print_r($_POST['events']);
的结果将是这样的:所以你想做的是给出您的复选框具有相同的
name=
属性和不同的value=
属性,它们对应于您想要从用户处获取的事件 ID。然后提交的数组的值将对应于所选的选项。If I follow what you are trying to do here, I think you are trying to collect an array of the checked checkboxes. Consider the following:
If I check 1,2, and 5, the result of
print_r($_POST['events']);
will be something like this:So what you want to be doing is giving your checkboxes all the same
name=
attribute, and differentvalue=
attributes, that correspond to the event id's that you want to get from the user. Then the values of the submitted array will correspond to the selected options.$event1[] = ...
会将一个值添加到数组$event1
中。如果$event1
尚未包含数组,则会创建一个数组。我认为这就是你问题的核心。将表单输入命名为“event[]”(或任何其他带括号的名称)背后的想法是 PHP 会将其解释为数组。这样,您就可以在单个(数组)值中拥有整个系列的输入。如果你们每个人都以不同的方式命名它们,那就没有意义了。我认为这样做更好,但一定不要使用括号。
$event1[] = ...
will ad a value to the array$event1
. An array is created if$event1
didn't already contain one. I think that's the core of your problem.The idea behind naming a form input 'event[]' (or any other name with brackets) is that PHP will interpret it as an array. That way, you can have a whole series of inputs in a single (array) value. It doesn't make sense if you each name them differently. I think it is better to do so, but you must not use the brackets.
每行的 HTML 看起来像这样:
当提交到页面时,这会将行的 ID 放入数组中,然后您可以像这样从请求中检索该 ID:
现在,您可以循环遍历 $event1 数组,其中的键对应于已选中其复选框的 ID。
Your HTML for each row can look something like this:
This puts the ID of the row into the array when it is submitted to the page, which you would then retrieve from the request like this:
Now, you can loop through the $event1 array, which will have keys corresponding to the IDs that had their checkboxes checked.