开发者

Problems updating mySQL DB

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 开发者_JAVA百科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


If I understand correctly, you have a table of about 100 rows with:

id, first name, last name, event 1, event 2, event 3

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:

<input type='checkbox' name='event1[]' value='3'>


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:

<input type="checkbox" name="events[]" value="1" />
<input type="checkbox" name="events[]" value="2" />
<input type="checkbox" name="events[]" value="3" />
<input type="checkbox" name="events[]" value="4" />
<input type="checkbox" name="events[]" value="5" />

If I check 1,2, and 5, the result of print_r($_POST['events']); will be something like this:

Array (
  0 => 1
  1 => 2
  2 => 5
)

So what you want to be doing is giving your checkboxes all the same name= attribute, and different value= 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[] = ... 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.


Your HTML for each row can look something like this:

echo '<input type="checkbox" name="event1[' . $row['ref'] .']" value="1" />';

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:

$event1 = $_POST['event1'];

Now, you can loop through the $event1 array, which will have keys corresponding to the IDs that had their checkboxes checked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜